PHP gets and deletes a directory and all file instances in the directory_PHP tutorial

WBOY
Release: 2016-07-13 10:49:21
Original
968 people have browsed it

Today we will take a look at getting a directory and deleting files in the directory or file instances in the specified directory in php. I hope this method will be helpful to all my friends.

Deleting directories and files using php programs has many advantages over deleting via ftp. First of all, it is troublesome to enter ftp. Then when deleting a large number of files, ftp deletion is very slow. Also, the program only wants to delete files under the folder while retaining the directory structure. This can be easily achieved with php, and there are also generated logs. , it is obviously much easier to clear the cache with a program.

PHP gets and deletes a directory and all file instances in the directory_PHP tutorial

The first step is to get all the files and folders under the directory with PHP

The code is as follows Copy code
 代码如下 复制代码

 $dir = "D:/";  //要获取的目录

 echo "********** 获取目录下所有文件和文件夹 ***********


";

 //先判断指定的路径是不是一个文件夹

 if (is_dir($dir)){

  if ($dh = opendir($dir)){

   while (($file = readdir($dh))!= false){

    //文件名的全路径 包含文件名

    $filePath = $dir.$file;

    //获取文件修改时间

    $fmt = filemtime($filePath);

    echo "(".date("Y-m-d H:i:s",$fmt).") ".$filePath."
";

   }

   closedir($dh);

  }

 }

?>


$dir = "D:/"; ​​//The directory to be obtained
 代码如下 复制代码


function delDirAndFile( $dirName ){ 

 if ( $handle = opendir( "$dirName" ) ) { 

  while ( false !== ( $item = readdir( $handle ) ) ) { 

   if ( $item != "." && $item != ".." ) { 

     if ( is_dir( "$dirName/$item" ) ) { 

       delDirAndFile( "$dirName/$item" ); 

     } else { 

       if( unlink( "$dirName/$item" ) )echo "已删除文件: $dirName/$item
n"; 

     } 

    } 

   } 

  closedir( $handle ); 

  if( rmdir( $dirName ) ) echo "成功删除目录: $dirName
n"; 

 } 

}

 

echo "************ Get all files and folders in the directory *************
"; //First determine whether the specified path is a folder if (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh))!= false){ //The full path of the file name including the file name $filePath = $dir.$file; //Get file modification time $fmt = filemtime($filePath); echo "(".date("Y-m-d H:i:s",$fmt).") ".$filePath."< ;br/>"; } closedir($dh); } } ?>
Step 2: Delete the directory and all files in the directory
The code is as follows Copy code
function delDirAndFile( $dirName ){ if ( $handle = opendir( "$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { delDirAndFile( "$dirName/$item" ); } else { If( unlink( "$dirName/$item" ) ) echo "Deleted file: $dirName/$item
n"; } } } closedir( $handle ); if( rmdir( $dirName ) ) echo "Directory deleted successfully: $dirName
n"; } }


Step 3: Delete the files under the directory without deleting the directory

if ( $handle = opendir( "$dirName" ) ) {
The code is as follows
 代码如下 复制代码


function delFileUnderDir( $dirName ){ 

 if ( $handle = opendir( "$dirName" ) ) { 

  while ( false !== ( $item = readdir( $handle ) ) ) { 

   if ( $item != "." && $item != ".." ) { 

    if ( is_dir( "$dirName/$item" ) ) { 

      delFileUnderDir( "$dirName/$item" ); 

     } else { 

      if( unlink( "$dirName/$item" ) ) echo "已删除文件:$dirName/$item
n"; 

     } 

    } 

   } 

  closedir( $handle ); 

 } 

}

Copy code

function delFileUnderDir( $dirName ){
while ( false !== ( $item = readdir( $handle ) ) ) {

if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { delFileUnderDir( "$dirName/$item" ); } else { if( unlink( "$dirName/$item" ) ) echo "Deleted file: $dirName/$item
n"; } }
}
closedir( $handle );
} } Summary For this we first start by getting all the files and directories in the directory and then delete them based on that. http://www.bkjia.com/PHPjc/632719.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632719.htmlTechArticleToday we will take a look at getting the directory and deleting the files in the directory or the file instances in the specified directory in php. I hope This method will be helpful to all my friends. Use php program to delete directories and files...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!