Auto delete all files after x-time

后端 未结 8 1400
春和景丽
春和景丽 2020-12-28 10:04

How do you auto delete all files under a sub directory after x-time (let say after 24 hours) - without using a cronjob command from server or pl. How can you do this just us

8条回答
  •  臣服心动
    2020-12-28 10:11

    function deleteCachedData($hours=24)
    {
        $files = glob(ROOTDIR.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.'*.txt');
        foreach($files as $file) {
            if(is_file($file) && (time() - filectime($file)) > $hours*3600) {
                unlink($file);
            }
        }
    

    }

提交回复
热议问题