Home > Backend Development > PHP Tutorial > An example of PHP code to delete all files in a directory N days ago

An example of PHP code to delete all files in a directory N days ago

WBOY
Release: 2016-07-25 08:59:49
Original
1417 people have browsed it
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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template