简体   繁体   中英

How to delete files in a folder on hosting after 2 hours interval

请帮助我...我想删除用户登录我的网站后生成的图像文件....这些文件保存在网站的文件夹中,所以我基本上希望该特定文件夹中的所有文件每次删除后都被删除间隔2小时

Use cron job. Tell it to list the files, and if they are older than 2 hours, use unlink() to remove them.

$dir = "/var/www/userimages/";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ((time() - filemtime($file)) > strtotime('2 hours')) {
                unlink($file);
            }
        }
        closedir($dh);
    }
}

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