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;
}
Today, I wrote a file operation class. One of the functions that delete files and folders reports an error each time. After checking, I only need to set rmdir ($ delList). This is normal after the file handle is closed.
Correct function:
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;
}