1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Auto Delete Files In Folder

Discussion in 'PHP' started by WizardRSS, May 10, 2010.

  1. #1
    I am looking for a way to auto delete files in a certain folder if they have not been accessed in X amount of hours. For example, if a certain file has not been accessed within 8 hours, it should be automatically deleted.

    I am using a cPanel server on a shared hosting account.
     
    WizardRSS, May 10, 2010 IP
  2. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to use a cron job on the server that runs a script to check the last access date of all the files in the folder and delete old files.
     
    musicmasteria, May 10, 2010 IP
  3. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's the code to check the files (untested). You just need to test it out and if it works create a cron job to run in at whatever interval you like.

    
    <?php 
    $age = 3600; //Set how old a file must be to delete, in seconds (3600 secs is 10 mins)
    $time = time(); // Get current time
    $dir = '/path/to/files'; // set directory of files to be checked
    
    //Check for files
    if ($handle = opendir($dir)) {
    	//loop through files
    	while (false !== ($file = readdir($handle))) {
    		$lastAccess = fileatime($file); //Get last access time
    		if( ($time - $lastAccess) >= $age ) //check age
    			unset($file); // delete old files
    	}
    	closedir($handle);
    }
    ?>
    
    PHP:
     
    musicmasteria, May 10, 2010 IP
  4. WizardRSS

    WizardRSS Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I just tried running that code and got about 1000 lines of the following error:

    Warning: fileatime() [function.fileatime]: stat failed for FileToBeDelete.html in /home/public_html/mydomain.com/cron.php on line 10
     
    Last edited: May 10, 2010
    WizardRSS, May 10, 2010 IP
  5. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this script:
    
    <?php
    $age = 3600; //Set how old a file must be to delete, in seconds (3600 secs is 10 mins)
    $time = time(); // Get current time
    $dir = '/path/to/files'; // set directory of files to be checked, no trailing slash
    
    //Check for files
    if ($handle = opendir($dir)) {
        //loop through files
        while (false !== ($file = readdir($handle))) {
            $filepath = $dir . '/' . $file;
            $lastAccess = fileatime($filepath); //Get last access time
            if( $lastAccess !== false ) // if fileatime didn't fail
                if( ($time - $lastAccess) >= $age ) //check age
                    unset($filepath); // delete old files
        }
        closedir($handle);
    }
    ?>
    
    PHP:
     
    musicmasteria, May 10, 2010 IP
  6. Qc4

    Qc4 Peon

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    On line 14, I think you meant unlink(), not unset(). ;)
     
    Qc4, May 11, 2010 IP
  7. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ah yes I did. Sorry about that. unset() destroys the variable, unlink() deletes the file.
    Thanks for catching that Qc4
     
    musicmasteria, May 11, 2010 IP
  8. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I wouldn't try it on your server. Make a small folder with 10 files or so. if you got 1000 errors, that means the script looped 1000 times, aka 1000 files. are you sure you want all those to be deleted?

    Yes, do a cronjobs. it will run your script periodically, however you set it up.
     
    Erind, May 11, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    You could always add:
    $i++;
    if ($i >= $max) break; 
    PHP:
    within the while loop.
     
    danx10, May 11, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Here's the Script
    I hope it will work

     
    roopajyothi, May 11, 2010 IP
  11. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry but your code won't work. You might want to go over how loops and increments work in php.

    '$mdel = $i++;' should be '$mdel++;' and it must be inside the loop (in the check age if statement block) to increment each time a file is deleted.
    Also, why add a limit the loop when no limit was requested?
     
    musicmasteria, May 11, 2010 IP
  12. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Here's the updated code.

    
    <?php
    $age = 28800; //Set how old a file must be to delete, in seconds (28800 secs is 8 hours)
    $time = time(); // Get current time
    $dir = '/path/to/files'; // set directory of files to be checked, no trailing slash
    
    //Check for files
    if ($handle = opendir($dir)) {
        //loop through files
        while (false !== ($file = readdir($handle))) {
            $filepath = $dir . '/' . $file;
            $lastAccess = fileatime($filepath); //Get last access time
            if( $lastAccess !== false ) // if fileatime didn't fail
                if( ($time - $lastAccess) >= $age ) //check age
                    unlink($filepath); // delete old files
        }
        closedir($handle);
    }
    ?>
    
    PHP:
     
    musicmasteria, May 11, 2010 IP
  13. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #13
    Is this right??
     
    roopajyothi, May 11, 2010 IP
  14. musicmasteria

    musicmasteria Peon

    Messages:
    33
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Well apart from not initializing $i it's a valid code but it doesn't have your desired effect. The code you posted will limit the number of files tested to just 10 files rather than limit the number of files deleted to 10. HOWEVER in this case a limit is NOT a desired effect. If a limit was present then the number of old files in the directory would build up and cause the whole script (and the site running the script) to slow down.

    In this case you would want all old files, regardless of how many there are, to be deleted.

    I think it would be best if we continue this php lesson in PMs as it has taken over this thread (which is against the rules).

    EDIT: Oh I didn't catch it before but unset() should be unlink(). Thx Qc4
     
    Last edited: May 11, 2010
    musicmasteria, May 11, 2010 IP
  15. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #15
    Instead of looping continuously we can set 1000 as a maximum number
    then it will be enough and prevents server load too.
    Alternative is to schedule using the cron jobs when there a large number of files
     
    roopajyothi, May 11, 2010 IP