Jump to content

auto delete


cavendano

Recommended Posts

This script was meant to delete a file after X days...in this case I wanted it to delete files that were older than 1 day except .htaccess. 

what the script is doing is deleting everything in the specified folders except htaccess even though the files are less than a day old.

any suggestions?

 

<?php
function delete_old_files($dir, $time_limit = 0,$exclude=array()){ 
if(!$time_limit) $time_limit = 1*24*60*60; 
if ($df = opendir($dir)) {  
while (false !== ($file = readdir($df))) {
   if ($file != "." && $file != "..") {    
$last_modified = filemtime($file);       
if((time()-$last_modified > $time_limit) && !in_array($file , $exclude)){     unlink($dir."/".$file);   
}   
}  
}  closedir($df); 
}
}delete_old_files('gcd' , 10 , array('.htaccess'));
delete_old_files('uploadedFiles' );
delete_old_files('temp' );
?>

Link to comment
https://forums.phpfreaks.com/topic/72634-auto-delete/
Share on other sites

its not clear what folder you are seeing the function delete anything,

but looking at your code you call the function 3 times, the last two of which will use your default time_limit of 0 days.

looking at your function i think you mean to have the time limit be null not 0, 0 could be assumed a numeric value vs false

 

if you provide more details into what folder is having the issue we might have more insight for ya

 

good luck

Link to comment
https://forums.phpfreaks.com/topic/72634-auto-delete/#findComment-366232
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.