Home  >  Article  >  Backend Development  >  Recursively delete directories and files in rrmdir php_PHP tutorial

Recursively delete directories and files in rrmdir php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:45999browse

Copy code The code is 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
Report a bug Description
bool rmdir (string $dirname)
Try to delete the directory specified by dirname. The directory must be empty and must have appropriate permissions. Returns TRUE on success, or FALSE on failure.
Note: As of PHP 5.0.0 rmdir() can also be used with certain URL wrapping protocols. See the list of Supported Protocols and Wrappers to see which URL wrapping protocols rmdir() supports.
Note: Support for Context was added in PHP 5.0.0. See the Stream function for a description of context.
Note: When safe mode is enabled, PHP will check whether the directory being scripted has the same UID (owner) as the script being executed when executing the script.
See mkdir() and unlink().
Copy code The code is 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($dir);
}
}
?>

This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.
Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well. 🎜>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

The code is as follows:


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


For more instructions, please refer to http://cn.php.net/rmdir



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

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/323338.html
TechArticle

Copy the code The code is as follows: function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != “.” $object != “..”) { if (f...
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