Home >Backend Development >PHP Problem >How to delete files by time in PHP?
#How to delete files by time in PHP?
First use the function "opendir()" to read all files in the folder; then use the function "is_dir()" to filter out the folder; then use the function "filemtime()" to get the file creation time; finally delete it according to the creation time.
Code example
<?php /* * 删除文件夹下$n分钟前创建的文件 * @param $dir 要处理的目录,物理路径,结尾不加\ * @param $n 过期时间,单位为分钟 * @return void */ function del_file_by_ctime($dir,$n){ if(is_dir($dir)){ if($dh=opendir($dir)){ while (false !== ($file = readdir($dh))){ if($file!="." && $file!=".."){ $fullpath=$dir."/".$file; if(!is_dir($fullpath)){ $filedate=filemtime($fullpath); $minutes=round((time()-$filedate)/60); if($minutes>$n) unlink($fullpath); //删除文件 } } } } closedir($dh); } } //下面是调用的代码 //删除1天前的文件 $dir = realpath('./Upload/export'); del_file_by_ctime($dir, 24*60); ?>
Recommended tutorial: "PHP"
The above is the detailed content of How to delete files by time in PHP?. For more information, please follow other related articles on the PHP Chinese website!