How to delete all files in a folder after a certain time
I am using this code to delete all files in a folder
$dir = $_SERVER['DOCUMENT_ROOT'].'/upload/';
$op_dir=opendir($dir);
$x = 5;
$current_time = time();
$difference = $current_time - $x;
while($file=readdir($op_dir ))
{
if($file != "." && $file != ".." ){
var_dump($dir.$file);
unlink ($dir.$file);
}
}
closedir($dir);
But I need to do this action after a certain time, for example, after 6 months, all files in the folder should be deleted. I have searched more, but all codes are related to the file creation date, but not the usual specific time.
+3
aidadev
source
to share
2 answers
You can execute the action as a cronjob. Read the following link: Crontab
You can also store the date in the database and consult at the top of the script if 6 months have passed.
+1
Tobias
source
to share
How to calculate the start time for 6 months so that we can take the catalog creation time or save the specific time in the database for calculation. The best option is a cronjob for this task.
0
ASH
source
to share