function delFile($delList, $deleteRootToo){
if(is_dir($delList)){
@chmod($delList, 0777);
if(!$dh = @opendir($delList)){
return false;
}
while (false !== ($file = readdir($dh))) {
if($file == '.' || $file == '..') continue;
if(is_file($delList.'/'.$file)){
@unlink($delList.'/'.$file);
}else{
$this->delFile($delList.'/'.$file, $deleteRootToo);
}
if($deleteRootToo) rmdir($delList);
}
}else if(is_file($delList)){
@unlink($delList);
}else{
return false;
}
closedir($dh);
return true;
}
今天寫了個檔案操作類 其中一個刪除檔案和檔案夾的函數每次都報錯,經過檢查,只要將rmdir($delList);這個放在關閉檔案控制代碼後就正常了
正確函數:
function delFile($delList, $deleteRootToo){
if(is_dir($delList)){
@chmod($delList, 0777);
if(!$dh = @opendir($delList)){
return false;
}
while (false !== ($file = readdir($dh))) {
if($file == '.' || $file == '..') continue;
if(is_file($delList.'/'.$file)){
@unlink($delList.'/'.$file);
}else{
$this->delFile($delList.'/'.$file, $deleteRootToo);
}
}
}else if(is_file($delList)){
@unlink($delList);
}else{
return false;
}
closedir($dh);
if($deleteRootToo) rmdir($delList);
return true;
}