Home > Backend Development > PHP Problem > How to delete all folders in a directory in php

How to delete all folders in a directory in php

coldplay.xixi
Release: 2023-03-08 07:22:01
Original
2323 people have browsed it

php method to delete all folders in a directory: first delete the files in the directory, the code is [$dh=opendir($dir)]; then delete the current folder, the code is [if(rmdir($ dir))].

How to delete all folders in a directory in php

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer. This method is suitable for all brands of computers.

php method to delete all folders in a directory:

<?
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

Related video recommendations: PHP Programming From beginner to master

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

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