0

So I have this function I am using to delete files and subdirectories recursively. It can also delete single and an array of files, though that is not the issue. Everything works fine up until it has to delete the directories, all levels of files get deleted, yet folders will not. It will however delete the target directory if no subdirectories exist. Not sure if it's a directory handle issue, or what at this point. It is not a permissions issue although.

    public static function delete($path, $files){
    $dirHandle = opendir($path);
    if($files === '*'){ //delete all files and subdirectories
        foreach(glob("{$path}/*") as $file){
            if(self::checkExist($file, 'folder')){
                $pdp = dirname($file) . '/';
                $file = basename($file);
                self::delete($pdp, $file);
            } else {
                if(!unlink($file)){
                    //flash message: 'Sorry, something went wrong. Please contact an administrator.'
                    //log error couldnt delete file: $file
                }
            }
        }
        if(count(glob("{$path}/*.*", GLOB_BRACE)) === 0){
            closedir($dirHandle);
            if(!rmdir($path)){
                //flash message: 'Sorry, something went wrong. Please contact an administrator.'
                //log error couldnt remove directory: $path
            }
        }
        return true;
    }}

I also need to keep the checks and space for error logging, my entire Class, keeps this code 'theme' consistently.

  • Are you getting an error? Is `rmdir` returning false? Is it getting to `rmdir`? – Blue Aug 06 '16 at 05:54
  • 1
    So are you getting an error? Is `error_reporting` on? PHP should throw an error if it is unable to remove the directory – Blue Aug 06 '16 at 06:01
  • No errors, php.ini set to all, error reporting all. My guess is directory handle issue but ive tried a few things and no luck. Im pretty new to php so maybe its a noob mistake somehow. – EscanorSin7 Aug 06 '16 at 06:04
  • Is `rmdir` returning true every time? You have comments to flash an error...is an error happening? – Blue Aug 06 '16 at 06:11
  • No errors except the rmdir one, ive tested everywhere else you can think of in the code for errors. rmdir doesn't return true except for when it can delete the parent with no subdirectories. Im stumped, it should work when running on the children just like the parent. – EscanorSin7 Aug 06 '16 at 06:17
  • Why do you use `glob("{$path}/*")` for deleting files, and then `glob("{$path}/*.*", GLOB_BRACE)` to check if any files are left? In my opinion they should be the same, and if they are, the second one is not needed. Also check the number of `/` after the path. You seem to add two on recursion. – KIKO Software Aug 06 '16 at 06:54
  • the first one is used in the loop for returning an array of every file/dir, the second on is a check to make sure that the dir being deleted is empty, not needed but ill make use of it later. and all paths come out as they should. ive checked to make sure no doubles and stuff was going on. – EscanorSin7 Aug 06 '16 at 07:08
  • 1
    About the double slashes: You use `$pdp = dirname($file).'/';` as the path you recurse with. Then inside `delete()` you add again a slash to `$path` inside `glob("{$path}/*")`, that makes two slashes, I think? – KIKO Software Aug 06 '16 at 07:11
  • Why not use directory iterators? http://stackoverflow.com/a/3352564/2870598 – Christian Aug 06 '16 at 07:27
  • Kiko you were so right sorry! I guess after all them ours working on this class I was a bit tired and irritated. But that wasn't the only issue, I had the second argument for the recursive delete set to the file instead of * for all. Although now the browser just hangs on loading. It deletes some files then hangs. Getting close though. – EscanorSin7 Aug 07 '16 at 20:42

0 Answers0