Copy Code code as follows:
function Rrmdir ($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-Delete Directory
Statement 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 some URL encapsulation protocols. See the list of supported protocols and Wrappers to see which 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 Safe mode is enabled, PHP checks to see if the directory being scripted will have the same UID (owner) as the script being executed when the script is executed.
See MkDir () and unlink ().
Copy Code code as follows:
<?php
function Rrmdir ($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 is isn ' t my code, but just thought I would share, since it took me. This is a function to delete a folder, all sub-folders, and files in one.
Just tell it what directory for your want deleted, in relation to the the page so this function is executed. Then Set $empty = True if you are want the folder just emptied, but not deleted. If you have set $empty = False, or just simply leave it out, the given directory would be deleted, as.
Copy Code code as follows:
<?php
function DeleteAll ($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:
Copy Code code as follows:
<?php
Delete folder function
function DeleteDirectory ($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);
}
?>
More instructions can refer to Http://cn.php.net/rmdir