开发者

PHP recursive delete function gives warning message

开发者 https://www.devze.com 2023-04-12 03:18 出处:网络
My function cleanup looks like that. function cleanUp($exdirs, $exfiles){ $it = new RecursiveIteratorIterator(

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消