dk4210 Posted November 11, 2010 Share Posted November 11, 2010 Hi guys, I using the following code if ($handle = opendir('temp_photo')) { while (false !== ($file = readdir($handle))) { $filelastmodified = filemtime($file); if(($filelastmodified-time()) > 24*3600) { unlink($file); } } closedir($handle); } I want it to look in the directory and remove all the files that are older than 24 hours old. When I run the code I get the following error.. Warning: filemtime() [function.filemtime]: stat failed for image1.jpg Warning: filemtime() [function.filemtime]: stat failed for image2.jpg Warning: filemtime() [function.filemtime]: stat failed for image3.jpg Warning: filemtime() [function.filemtime]: stat failed for image4.jpg It seems to read the directory, but it doesn't remove them and I receive the error message. Any ideas of what may be going on or do you have a script that may resolve this issue? Thanks, Dan Quote Link to comment Share on other sites More sharing options...
Adam Posted November 11, 2010 Share Posted November 11, 2010 readdir returns the file name, not including the file path. Therefore $file alone doesn't exist. You'd need to use: 'temp_photo/' . $file .. glob I reckon would be better here though (as it does return the path): foreach (glob('./temp_photo/*') as $file) { $filelastmodified = filemtime($file); if(($filelastmodified-time()) > 24*3600) { unlink($file); } } Not tested but should work. Quote Link to comment Share on other sites More sharing options...
dk4210 Posted November 11, 2010 Author Share Posted November 11, 2010 Hi Adam, I tried the code you gave me and it doesn't give me the error message but it also doesn't removes any of the files.. They're still there even though I run the script.. Any more ideas? Quote Link to comment Share on other sites More sharing options...
requinix Posted November 11, 2010 Share Posted November 11, 2010 If you want this to run automatically, just make a cron job using find(1): find /path -type f -mtime +0 -delete Quote Link to comment Share on other sites More sharing options...
dk4210 Posted November 11, 2010 Author Share Posted November 11, 2010 I would rather do it in the code as opposed to the cron job I am also trying this code, but it doesn't seem to work either // Define the folder to clean // (keep trailing slashes) $checktfolder = 'temp_photo/'; // Filetypes to check (you can also use *.*) foreach (glob("temp_photo/*.*") as $fileTypes); // $fileTypes = $filetypes2; // Here you can define after how many // minutes the files should get deleted $expire_time = 2; // Find all files of the given file type foreach (glob($checktfolder . $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); } } Quote Link to comment Share on other sites More sharing options...
Rifts Posted November 11, 2010 Share Posted November 11, 2010 This may not help at all but its worth throwing out there. Upon uploading the image I would have to saved to a DB with 3 rows: Id Name Date then in your script you can just check which are older then a day and unlink them by name Quote Link to comment Share on other sites More sharing options...
brianlange Posted November 12, 2010 Share Posted November 12, 2010 are you sure your if statement ever evaluates to true? Make sure that is happening before looking as to whether the files are being deleted. This will never evaluate to true since $filelastmodified - time() will always be a negative number. if(($filelastmodified-time()) > 24*3600) { unlink($file); } Quote Link to comment Share on other sites More sharing options...
dk4210 Posted November 12, 2010 Author Share Posted November 12, 2010 Thanks guys for the expert help.. I finally got it to work with the following code $expiretime=3600; //expire time in minutes $tmpFolder="temp_photo/"; $fileTypes="*.*"; foreach (glob($tmpFolder . $fileTypes) as $Filename2) { echo "$Filename2<br>"; // Read file creation time $FileCreationTime = filectime($Filename2); // Calculate file age in seconds $FileAge = time() - $FileCreationTime; // Is the file older than the given time span? if ($FileAge > ($expiretime * 60)){ // Now do something with the olders files... //print "The file $Filename2 is older than $expire_time minutes\n"; //deleting files: unlink($Filename2); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.