Delete temporary files

This example shows how to delete specific files after a given time span.
This works good for cleaning cached files.

// Define the folder to clean
// (keep trailing slashes)
$captchaFolder  = 'temp/';
 
// Filetypes to check (you can also use *.*)
$fileTypes      = '*.jpg';
 
// Here you can define after how many
// minutes the files should get deleted
$expire_time    = 20; 
 
// Find all files of the given file type
foreach (glob($captchaFolder . $fileTypes) as $Filename) {
 
    // Read file creation time
    $FileCreationTime = filectime($Filename);
 
    // Calculate file age in seconds
    $FileAge = time() - $FileCreationTime; 
 
    // Is the file older than the given time span?
    if ($FileAge > ($expire_time * 60)){
 
        // Now do something with the olders files...
 
        print "The file $Filename is older than $expire_time minutes\n";
 
        // For example deleting files:
        //unlink($Filename);
    }
 
}
Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
12/10/2007

Updated:
12/10/2007

Tags:
, ,

Related links:
- Glob examples




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

Chris July 07, 2011 at 22:51
This is a wonderful script, but when would you trigger this?
ringo June 07, 2011 at 12:44
thank you so much for your help it's a great script and easy to use, owowowo you are a life savior
Gergő Gyula May 17, 2011 at 22:49
Great code, thanks!!!
carl May 16, 2011 at 23:27
this is great, just what I needed. thanx
I just had to add a ."*". in the middle of the glob function call.
paco March 24, 2011 at 12:03
My os is win 7 64bit, this does not work for me.
regan March 24, 2011 at 02:26
I would use filemtime() function to get the modified timestamp. filectime gets the inode change time so anything will update this value (like a permission change for example).
paco March 23, 2011 at 09:50
i use wamp as localhost,upload as my upload folder but can`t get this to work,where would i have to insert upload ? and i`d like to delete all the files in my upload folder after 3 hours,please help.
jt whissel March 19, 2010 at 02:12
IS this only for linux? I am trying to do it on a windows box server. and I am have it delete php.tmp files from a folder and it will not do it.

<?php


// Define the folder to clean
// (keep trailing slashes)
$captchaFolder = 'E:temp';

// Filetypes to check (you can also use *.*)
$fileTypes = '*.*';

// Here you can define after how many
// minutes the files should get deleted
$expire_time = 5;



// Find all files of the given file type
foreach (glob($captchaFolder . $fileTypes) as $Filename) {

// Read file creation time
$FileCreationTime = filectime($Filename);

// Calculate file age in seconds
$FileAge = time() - $FileCreationTime;



// Is the file older than the given time span?
if ($FileAge > ($expire_time * 60)){

// Now do something with the olders files...

print "The file $Filename is older than $expire_time minutesn";

// For example deleting files:
unlink($Filename);
}

}

echo 'ran';
?>
SQ257 February 12, 2010 at 00:16
Thanks a bunch this is just what i needed.
Tan December 01, 2009 at 22:40
Great work, but it throws warning if $captchaFolder is empty! Requires one more check before "glob".
aap November 23, 2009 at 10:53
This is perfect! Exactly what i needed! Many thanx for this script...
Testing March 13, 2009 at 03:41
This works perfectly. I have a script I wrote that creates a file that is then included as an instant result to the user. I had files piling up like crazy. I added this at the bottom of the page and it deleted all my old, unneeded files. Thanks so much!
ad February 01, 2009 at 20:49
asd