Home > php教程 > PHP开发 > body text

Recursively delete directories and files in rrmdir php

高洛峰
Release: 2016-12-28 15:02:31
Original
1457 people have browsed it

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); 
} 
}
Copy after login

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: Since 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 (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().

<?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); 
} 
} 
?>
Copy after login
<?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 != &#39;.&#39; && $contents != &#39;..&#39;) { 
$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 after login
<?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 == &#39;.&#39; || $item == &#39;..&#39;) continue; 
if (!deleteDirectory($dir . "/" . $item)) { 
chmod($dir . "/" . $item, 0777); 
if (!deleteDirectory($dir . "/" . $item)) return false; 
}; 
} 
return rmdir($dir); 
} 
?>
Copy after login

For more articles related to recursively deleting directories and files in directories in rrmdir php, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!