Let me introduce to you a php code that can delete all files in a specified directory N days ago. Friends in need can refer to it.
The code is as follows:
<?php
/**
* 删除目录下N天前所有文件
* by http://bbs.it-home.org
*/
function delfile($dir,$n) //删除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=date("Y-m-d", filemtime($fullpath));
$d1=strtotime(date("Y-m-d"));
$d2=strtotime($filedate);
$Days=round(($d1-$d2)/3600/24);
if($Days>$n)
unlink($fullpath); ////删除文件
}
}
}
}
closedir($dh);
}
}
?> Copy after login
Articles you may be interested in:
How to delete specified files and folders in Php
Small example of PHP deleting all files created N minutes ago
php example: batch delete folders and files in folders
Example of how to delete a directory and all files in php
PHP directory traversal and deletion function examples
Three ways to delete a directory using php rmdir
An example of PHP directory traversal and deletion code
Delete the php code of all files in the specified folder
Delete php custom function for multi-level directories
php code to delete a directory and list all files in the directory
php code for recursively deleting files and directories
php code for recursively deleting all files in a directory and multi-level subdirectories
php code for recursively creating and deleting folders
Example of php recursively deleting directories
|