Home >Backend Development >PHP Tutorial >PHP recursively deletes directories_PHP tutorial

PHP recursively deletes directories_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:34:02867browse

Excerpted from “linuxer” blog http://deidara.blog.51cto.com/400447/118805

Deleting an empty directory is very simple ~ just use the

rmdir() function. However, if you want to delete a non-empty directory, you will not be able to delete it quickly. You must first delete the files in the directory. But there may be subdirectories in the directory, so recursive deletion is required~ Here is my example~


function deletedir($dir){
if(!handle=@opendir($dir)){ //Check whether the directory to be opened exists
die(" No directory ")); <">}
While (FALSE! == ($ File = Readdir ($ handle)) {
if ($ file! == "." && $ file! == ".."){ //Exclude the current directory and parent directory
                                                                                                                                                            🎜> deletedir($file);
                                                                                                                        echo "File$file was deleted successfully.
";

echo "file & lt; b & gt; $ file & lt;/b & gt; delete failure!"
                                                                                                               with with with  with with       with with echo "Directory$dir was deleted successfully.
";
} Else {
Echo" Directory & lt; b & gt; $ dir & lt;/b & gt; delete failure!
";
}
}

//Test program
$dir="/var/www/test";
deletedir($dir);
?>


Create a folder and file test under the /var/www/test folder
shell> touch aaa
shell> touch bbb
shell> touch ccc
shell> touch eee
shell> touch ffff
shell> mkdir 111

shell> mkdir 222

shell> mkdir 333
Create and write files in the 111, 222, 333 folders respectively. Say more, then give them permission
shell>chown [url]www.www[/url] test -R
Then open the program in IE and test it~~ Haha.





http://www.bkjia.com/PHPjc/508519.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/508519.html

TechArticleExcerpted from "linuxer" blog http://deidara.blog.51cto.com/400447/118805 To delete an empty The directory is very simple ~ one rmdir() function can do it, but to delete a non-empty directory,...
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