1

I'm trying to create function which removes all file and directories on webhosting excluding given files and folders arrays

function cleanUp($exdirs, $exfiles){
$it = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator('.'), 
  RecursiveIteratorIterator::CHILD_FIRST
);
foreach($it as $entry) {
  if ($entry->isDir() && !in_array($entry->getBasename(), $exdirs)) {
    rmdir($entry->getPathname());
  }
  else {
    if (!in_array($entry->getFileName(), $exfiles)) {
      unlink($entry->getPathname());
    }
    else {
      $exdirs[] = dirname($entry->getFileName());
    }
  }
}
}

And calling this function like this

$excludeDirsNames = array('cgi-bin');
$excludeFileNames = array('.htaccess', 'ws.zip', 'update.php');
cleanUp($excludeDirsNames , $excludeFileNames);

Now the problem is, it deletes but getting error message: Directory not empty on line rmdir($entry->getPathname()); several times. How to fix that problem?

3
  • Is this not here: stackoverflow.com/questions/7685134/…. As you are experiencing an issue with the previous answer, I would sugest you go back to that thread, deselect the answer you approved and ask questions there. If the answer you chose does not work, you should revert your choice so as to not confuse future users how look at the thread. Commented Oct 7, 2011 at 9:49
  • he gave me base idea. i can't deselect Commented Oct 7, 2011 at 9:50
  • i mean i don't want to deselect. He gave me main idea Commented Oct 7, 2011 at 9:52

2 Answers 2

1

You allow to exclude files and directories, but you don't test, if a directory contains other files, or directories, that were excluded before.

if (substr($oneExcludedFileOrDirectory, 0, strlen($currentDir) === $currentDir) {
  echo "Directory not empty";
}

Just a simple prefix comparison: Is "dir" is prefix of one of the excluded paths? Only works for absolute paths (and some other minor things), but it should explain, whats the matter.

Sign up to request clarification or add additional context in comments.

6 Comments

don't get it. how will look like your theory in my function
When /path/to/my/file starts with /path/to, then /path/to/my/file is in /path/to/ and when you don't delete /path/to/my/file, then /path/to is not empty.
Better? Now you should be able to integrate it into your code yourself.
it needs to be within foreach loop?
i really can't integrate it to my code. Please show me where it must be
|
0

Here are two reasons why it not work:

1) In one of your child-folder are files you are excluding. There are not deleted, so it is not possible to "rm" the folder.

2) After the "unlink" and "rmdir"-Funktion call "clearstatcache();". I think this will solve your problem. The files are delete, but the information is still available in the cache.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.