简体   繁体   中英

How to delete all files in folder after specific time

I am using this code to delete all files in 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 specific time for example after 6 month all files in the folder should be deleted.I searched more but all codes are related to file creation date not generally specific time.

You can execute the action as cronjob. Read the following link: Crontab

You also can save a date in a database and consult in top of the script if 6 month have elapsed.

如何计算6个月的开始时间,以便我们可以将目录创建时间或节省特定时间保存在数据库中进行计算。最佳选择是cronjob用于此任务

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM