0

I have tried two recursive scripts to wipe the contents of the folder:

Fist one:

function clearDirectory($path)
{
    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(get_home_path().$path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($files as $filename => $fileInfo) {
        if ($fileInfo->isDir()) {
            rmdir($filename);
        } else {
            unlink($filename);
        }
    }

}

Second one:

function rrdir($path)
{
    $src = get_home_path() . $path;
    $dir = opendir($src);
    while(false !== ( $file = readdir($dir)) ) {
        if (( $file != '.' ) && ( $file != '..' )) {
            $full = $src . '/' . $file;
            if ( is_dir($full) ) {
                rrmdir($full);
            }
            else {
                unlink($full);
            }
        }
    }
    closedir($dir);
    rmdir($src);
}

In both instances it does the same thing. But I encounter strange problem that it does not remove all files it only does so for 527 out of 706.

If I run any of these scripts twice it removes all of the files... But does not do so from the first time.

Has nothing to do with linux or any other permission tested and all permission are in order.

There are no errors thrown in logs everything works smoothly with these both scripts the problem is that they don't delete all files from the first time.

I am running windows 10 x64, with Vagrant and Homestead virtual box. Tested rm -f * it works fine. What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Andrius Solopovas
  • 967
  • 11
  • 41
  • 1
    Possible duplicate of [How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?](http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir) – wazelin Jul 20 '16 at 10:30
  • 1
    Problem in th first one, that you try too delete the folder with `rmdir()`, but it will only delete the folder if its empty, so they will skipped if not. – JustOnUnderMillions Jul 20 '16 at 10:33
  • 1
    is `rrmdir` wrong typo? – JustOnUnderMillions Jul 20 '16 at 10:35
  • sidenote: if you run the script from web the user will be `www-data` and if you are using a shell for `rm -f *` this will be another user like `root`. And maybe they have diffrent rights for delete/edit files. – JustOnUnderMillions Jul 20 '16 at 10:40
  • I agree with @JustOnUnderMillions that rmdir will only delete the folder if it's empty. You can try another alternative like shell_exec. For example: shell_exec('rm -rf ' . $filename); – Dianna Jul 20 '16 at 10:42
  • Guys the both scripts work, the problem is that it does not delete all files. There are no duplicates its 706 images that all needs to be deleted but both scripts only delete 527... – Andrius Solopovas Jul 20 '16 at 10:43
  • rm * also works. I did regenerate these files over and over again and when trying to delete it does not delete all of them... – Andrius Solopovas Jul 20 '16 at 10:44
  • @AndriusSolopovas did you see my comment above? + the documentation of rmdir on php.net: "Attempts to remove the directory named by dirname. The directory must be empty, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure." – Dianna Jul 20 '16 at 10:45
  • @Dianna you don't get the point. The issue that I have is that script skips 179 file inside the folder. And if I run same script twice it removes all of them. When I want to run it once. – Andrius Solopovas Jul 20 '16 at 10:46
  • Did you debug that, like: Are the last 179 files not deleted, or is this random. What then is different to the files that will deleted. If the functions work, what can we say from here. Something like: Maybe the pathes are too long for windows? – JustOnUnderMillions Jul 20 '16 at 10:53
  • How long takes that script? Is the time too short to delete all files on one run? `set_time_limit()` – JustOnUnderMillions Jul 20 '16 at 10:55
  • Are the files that are skipped containing any special characters or spaces ? – GreensterRox Jul 20 '16 at 10:58
  • I think i can see what the problem is: You call rrmdir($full) inside the function. But your function name is rrdir(). So you should change that and it will possibly work. – Dianna Jul 20 '16 at 10:59
  • Have you tried reviewing the output of the iterator itself? Perhaps the ordering is not as you expect, for instance. – Phylogenesis Jul 20 '16 at 11:02
  • @Dianna Both scripts work fine there is not any problems with them. They do delete the files but not all the files they delete the first 527 and leave 179. If I run same funtion again it deletes the remaining. – Andrius Solopovas Jul 20 '16 at 11:03
  • @AndriusSolopovas I already understood that. I just don't get where is the rrmdir function defined? Do you define it somewhere else? Cause I can't see how your code works if the rrmdir function doesn't exists. – Dianna Jul 20 '16 at 11:05
  • @AndriusSolopovas Just debug it like `if(!unlink($filename)) print "no unlink of $filename";` or `if(!unlink($full)) print "no unlink of $full";` – JustOnUnderMillions Jul 20 '16 at 11:09
  • @JustOnUnderMillions I have created $counter it returns only 527 files. So the iterator only sees 527 files rather than all 706 – Andrius Solopovas Jul 20 '16 at 12:05
  • @JustOnUnderMillions I have continued debugging and figured out that if set the counter and iterate through the folder without unlink or rmdir function. It returns the correct number of files but as soon as unlink function executed as there just files without sub directories it stop at 527 files. Like there is some sort of limiter. – Andrius Solopovas Jul 20 '16 at 13:03

2 Answers2

0

As also noted by JustOnUnderMillions, there are two problems:

  • clearDirectory calls rmdir, which requires the directory to be empty. You probably want to call rrdir, the other function.
  • The rrdir function calls rrmdir, which is undefined. You probably want to call rrdir here.
Daan Wilmer
  • 937
  • 4
  • 13
  • Man both scripts work fine. They do delete the files but not all it deletes first 527 and leaves 179. If I run it again it deletes the remaining. – Andrius Solopovas Jul 20 '16 at 11:01
0

Ok I think I found that the problem is not caused by the code. The problem probably caused by homestead virtual box that I use as my development environment, as when I setup xampp on my machine it worked like a charm. Thank you guys for trying to fix it.

Andrius Solopovas
  • 967
  • 11
  • 41