Rrmdir Delete directories and directories in PHP recursively

Source: Internet
Author: User

PHP comes with rmdir, can only delete empty directory, this rrmdir can be recursively delete directories and all files under the directory, but use to be careful oh, do not delete all the files
functionRrmdir ($dir) {if(Is_dir($dir)) {$objects=Scandir($dir);foreach($objects  as $object) {if($object! = "." &&$object! = ".") {if(filetype($dir.” /”.$object) = = "dir") Rrmdir ($dir.” /”.$object);Else unlink($dir.” /”.$object);}}Reset($objects);}} 

RmDir

(PHP 4, PHP 5)
rmdir-Deleting a directory
Report a bug description
BOOL RmDir (String $dirname)
An attempt was made to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Returns TRUE on success, or FALSE on failure.
Note: from PHP 5.0.0 rmdir () can also be used for certain URL encapsulation protocols. See the list of supported protocols and Wrappers to see what URL encapsulation protocols are supported by RmDir ().
Note: Added support for context in PHP 5.0.0. See the Stream function for a description of the context.
Note: When you enable Safe mode, PHP checks that the directory in which the script is executed will have the same UID (owner) as the script being executed when executing the script.
See MkDir () and unlink ().


<?PHPfunctionRrmdir ($dir) {if(Is_dir($dir)) {$objects=Scandir($dir);foreach($objects  as $object) {if($object! = "." &&$object!= "..") {if(filetype($dir." /".$object) = = "dir") Rrmdir ($dir." /".$object);Else unlink($dir." /".$object);}}Reset($objects);rmdir($dir);}}?>


This isn ' t my code, but just thought I would share, since it took me so long to find. This was a function to delete a folder, all sub-folders, and the files in one clean move.
Just Tell it how directory you want deleted, and the relation to the page and this function is executed. Then Set $empty = True if you want the folder just emptied, and not deleted. If you set $empty = False, or just simply leave it out, the given directory is deleted, as well.

<?PHPfunctionDeleteAll ($directory,$empty=false) {if(substr($directory,-1) = = "/") {$directory=substr($directory, 0,-1);}if(!file_exists($directory) || !Is_dir($directory)) {return false;} ElseIf(!is_readable($directory)) {return false;} Else {$directoryHandle=Opendir($directory); while($contents=Readdir($directoryHandle)) {if($contents! = '. ' &&$contents! = ' ... ') {$path=$directory. "/" .$contents;if(Is_dir($path) {deleteAll ($path);} Else {unlink($path);}}}Closedir($directoryHandle);if($empty==false) {if(!rmdir($directory)) {return false;}}return true;}}?>


[/code]
A Patch to previous script to make sure rights for deletion is set:

<?PHP//Delete folder functionfunctionDeleteDirectory ($dir) {if(!file_exists($dir))return true;if(!Is_dir($dir) ||Is_link($dir))return unlink($dir);foreach(Scandir($dir) as $item) {if($item= = '. ' | |$item= = '.. ')Continue;if(!deletedirectory ($dir. "/" .$item)) {chmod($dir. "/" .$item, 0777);if(!deletedirectory ($dir. "/" .$item))return false;};}return rmdir($dir);}?>

This article turns from http://www.jb51.net/article/27149.htm

Rrmdir Delete directories and directories in PHP recursively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.