Home > Backend Development > PHP Problem > 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
Release: 2023-03-01 09:52:01
Original
2720 people have browsed it

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;
  }
}
?>
Copy after login

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);
Copy after login

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!

Related labels:
php
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