rrmdir php中递归删除目录及目录下的文件
<div class="codetitle"><a style="CURSOR: pointer" data="47094" class="copybut" id="copybut47094" onclick="doCopy('code47094')"> 代码如下:
<div class="codebody" id="code47094">
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 —
删除目录
Report a bug 说明
bool rmdir ( string $dirname )
尝试
删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。成功时返回 TRUE, 或者在失败时返回 FALSE.
Note: 自
PHP 5.0.0 起 rmdir() 也可用于某些 URL 封装协议。参见Supported Protocols and Wrappers 的列表看看 rmdir()
支持哪些 URL 封装协议。
Note: 在
PHP 5.0.0 中
增加了 对上下文(Context)的
支持。有关 上下文(Context) 的说明参见 Stream
函数。
Note: 当启用 安全模式时,
PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
参见 mkdir() 和 unlink()。
<div class="codetitle">
<a style="CURSOR: pointer" data="38808" class="copybut" id="copybut38808" onclick="doCopy('code38808')"> 代码如下: <div class="codebody" id="code38808">
<?
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 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.
<div class="codetitle">
<a style="CURSOR: pointer" data="16782" class="copybut" id="copybut16782" onclick="doCopy('code16782')"> 代码如下: <div class="codebody" id="code16782">
<?
PHP function dele
teall($directory,$empty = false) {
if(substr($directory,-1) == "/") {
$directory = substr($directory,-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)) {
dele
teall($path);
} else {
unlink($path);
}
}
}
closedir($directoryHandle);
if($empty == false) {
if(!rmdir($directory)) {
return false;
}
}
return true;
}
}
?>
[/code]
A patch to prev
IoUs script to make sure rights for deletion is set:
<div class="codetitle">
<a style="CURSOR: pointer" data="56303" class="copybut" id="copybut56303" onclick="doCopy('code56303')"> 代码如下: <div class="codebody" id="code56303"> 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。