Home > Backend Development > PHP Tutorial > php delete entire directory through recursive function

php delete entire directory through recursive function

*文
Release: 2023-03-18 13:06:02
Original
1373 people have browsed it

How to delete an entire directory through recursive function in php? This article mainly introduces the recursive function implemented in PHP to delete the entire directory, using PHP recursive algorithm and directory operation skills. I hope to be helpful.

The example in this article describes the PHP implementation of a recursive function for deleting the entire directory. Share it with everyone for your reference. The specific implementation method is as follows:


<?php
function delete_directory($dir) {
   if ($dh = @opendir($dir)) {
     while (($file = readdir ($dh)) != false) {
       if (($file == ".") || ($file == "..")) continue;
        if (is_dir($dir . &#39;/&#39; . $file))
          delete_directory($dir . &#39;/&#39; . $file);
        else
          unlink($dir . &#39;/&#39; . $file);
     }
     @closedir($dh);
     rmdir($dir);
   }
}
$dir = "./fakeDir";
delete_directory($dir);
?>
Copy after login

Related recommendations:

php recursive analysis

php Three implementation methods of recursive functions

php Recursive statistics of the number of folders and files

The above is the detailed content of php delete entire directory through recursive function. 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