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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.