Php 函数不';我不想删除文件夹
除removeIt功能(空白页)外,所有功能均可工作。下面是我的代码:Php 函数不';我不想删除文件夹,php,function,class,rmdir,Php,Function,Class,Rmdir,除removeIt功能(空白页)外,所有功能均可工作。下面是我的代码: class Dir { public function emptyIt($path) { if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") {
class Dir {
public function emptyIt($path) {
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(is_file($path."/".$file)) {
unlink($path."/".$file);
} else {
if($handle2 = opendir($path."/".$file)) {
while (false !== ($file2 = readdir($handle2))) {
if ($file2 != "." && $file2 != "..") {
unlink($path."/".$file."/".$file2);
}
}
}
rmdir($path."/".$file);
}
}
}
}
return true;
}
function isEmpty($path) {
$handle=opendir($path);
$i=0;
while (false !== ($file = readdir($handle))) {
$i++;
}
closedir($handle);
if($i>=2) {
return false;
} else {
return true;
}
}
public function removeIt($path) {
if (emptyIt($path)) {
if (rmdir($path)) {
return true;
} else {
return false;
}
}
}
}
我有3个功能使其工作:
isEmpty
:验证文件夹是否为空emptyIt
:清空文件夹和子文件夹removeIt
:删除文件夹任何提示?尝试此操作将删除
文件夹及其内容(子文件夹
)
其中,$dir
是文件夹的路径
可能您使用没有权限对文件夹进行更改的用户执行程序,请尝试使用root用户执行该程序,或将权限授予当前用户,祝你好运。尝试此操作将不
删除空的目录轮胎
emptyIt
在找到文件夹时应递归调用自身,以防有更多级别的嵌套。@geedubb:否,无错误。
system('/bin/rm -rf ' . escapeshellarg($dir));
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);
}
}