Home  >  Article  >  Backend Development  >  How to delete a directory and files in the directory in php

How to delete a directory and files in the directory in php

WJ
WJOriginal
2020-06-01 11:04:512689browse

How to delete a directory and files in the directory in php

#php method to delete a directory and files under the directory:

First create a new php file "test.php" and add the following code:

<?
function deldir($dir) {
  //先删除目录下的文件:
  $dh=opendir($dir);
  while ($file=readdir($dh)) {
    if($file!="." && $file!="..") {
      $fullpath=$dir."/".$file;
      if(!is_dir($fullpath)) {
          unlink($fullpath);
      } else {
          deldir($fullpath);
      }
    }
  }
 
  closedir($dh);
  //删除当前文件夹:
  if(rmdir($dir)) {
    return true;
  } else {
    return false;
  }
}
?>

Then create the folder "test" in the same directory as test.php, and create subfiles or directories in its directory.

How to delete a directory and files in the directory in phpj Then add some code to "test.php" to delete the file:

function deldir(test);

Finally check whether the "test" directory has been deleted.

Related references:php中文网

The above is the detailed content of How to delete a directory and files in the directory in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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