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

php delete entire directory through recursive function

*文
*文Original
2017-12-26 09:58:311389browse

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

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!

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