PHP recursive delete function gives warning message
My function cleanup looks like that.
function cleanUp($exdirs, $exfiles){
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('.'),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach($it as $entry) {
if ($entry->isDir() && !in_array($entry->getBasename(), $exdirs)) {
try {
rmdir($entry->getPathname());
}
catch (Exception $ex) {
// dir not empty
}
}
elseif (!in_array($entry->getFileName(), $exfiles)) {
unlink($entry->getPathname());
}
}
}
And calling this function like that
$excludeDirsNames = array('cgi-bin');
$excludeFileNames = array('ws.zip');
cleanUp($excludeDirsNames , $excludeFileNames);
Now the problem is i'm getting warning message. can not unlink cgi-bin on line unlink($entry->getPathname());
What's wrong wit开发者_运维问答h my function? How to fix that problem?
I am guessing that cgi-bin
is a symlink and not a regular directory. That's why it's getting into the "unlink" section. The error message is probably due to permissions.
The fix, move 'cgi-bin' to the $excludeFileNames array.
精彩评论