Question

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 with my function? How to fix that problem?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top