Auto delete all files after x-time

后端 未结 8 1386
春和景丽
春和景丽 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:28

    Here is another example that uses GLOB and it will delete any file

    $files = glob('path/to/your/files/*');
    
    foreach($files as $file) { // iterate files
        // if file creation time is more than 5 minutes
        if ((time() - filectime($file)) > 3600) {  // 86400 = 60*60*24
            unlink($file);
        }
    }
    

    or if you want to exclude certain files

    $files = preg_grep('#\.txt#', glob('/path/to/your/files/*'), PREG_GREP_INVERT);
    

提交回复
热议问题