简体   繁体   English

如何在特定时间后删除文件夹中的所有文件

[英]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. 但是我需要在特定时间之后执行此操作,例如在6个月之后应该删除文件夹中的所有文件。我搜索了更多但是所有代码都与文件创建日期相关,而不是一般特定时间。

You can execute the action as cronjob. 您可以将此操作作为cronjob执行。 Read the following link: Crontab 阅读以下链接: Crontab

You also can save a date in a database and consult in top of the script if 6 month have elapsed. 您还可以在数据库中保存日期,如果已过去6个月,请在脚本顶部进行查阅。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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