Home >Backend Development >PHP Tutorial >PHP implements a recursive function for deleting an entire directory_PHP tutorial
This article mainly introduces the php implementation of the recursive function for deleting the entire directory, involving php recursive algorithm and directory operation skills , friends in need can refer to it
The example in this article describes the PHP implementation of a recursive function for deleting an entire directory. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
17
|
<๐>function delete_directory($dir) {<๐> <๐>if ($dh = @opendir($dir)) {<๐> <๐>while (($file = readdir ($dh)) != false) {<๐> <๐>if (($file == ".") || ($file == "..")) continue;<๐> <๐>if (is_dir($dir . '/' . $file))<๐> <๐>delete_directory($dir . '/' . $file);<๐> <๐>else<๐> <๐>unlink($dir . '/' . $file);<๐> <๐>}<๐> <๐>@closedir($dh);<๐> <๐>rmdir($dir);<๐> <๐>}<๐> <๐>}<๐> <๐>$dir = "./fakeDir";<๐> <๐>delete_directory($dir);<๐> <๐>?> |