-1

Below is my attempt to delete a folder and all its content. A folder may contain zip files and folders with files.

public function deleteFolder($dir){
    if(file_exists($dir)){
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
        $files = new \RecursiveIteratorIterator($it,
                         \RecursiveIteratorIterator::CHILD_FIRST);
        foreach($files as $file) {
            if ($file->isDir()){
                rmdir($file->getRealPath());
            } else {
                unlink($file->getRealPath());
            }
        }
        rmdir($dir);
    }           
}

but it returns the following error:

rmdir(C:\Juliver\UIUX\pd-loader\loader/temp/utso-pulgada-pd-loader-5066a7e0298a):

Directory not empty in C:\Juliver\UIUX\pd-loader\loader\Patcher.php on line 95

line 95 points to rmdir($dir); line

If I check the folder utso-pulgada-pd-loader-5066a7e0298a, I see that it's already empty but it throws me the above error.

Community
  • 1
  • 1
Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • 1
    Possible duplicate of [rmdir() Not Deleting an Empty Folder - PHP](https://stackoverflow.com/questions/7642655/rmdir-not-deleting-an-empty-folder-php) – treyBake Jul 18 '19 at 10:13
  • Or you could just execute a command, e.g. `rm -rf examplefolder` for *nix or `del /F/Q/S*.* > NULL` followed by `rmdir /Q/S examplefolder` for windows – LLJ97 Jul 18 '19 at 10:16
  • 1
    Possible duplicate of [How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP?](https://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir) – Bram Verstraten Jul 18 '19 at 11:03

3 Answers3

0
$dirname = 'C:/Users/Admin/Desktop/test';
array_map('unlink', glob("$dirname/*.*"));
rmdir($dirname);

try this, this remove all the file present in the folder, and that folder too

sayyed tabrez
  • 116
  • 1
  • 1
  • 11
0

Directory may contain other directories so you have to use a recursive function.

function removeDir($path) {
    $files = glob("$path/*");
    foreach ($files as $file) {
        if (is_dir($file)) {
            removeDir($file);
        } else {
            unlink($file);
        }
    }

    rmdir($path);
}

Now is enough to call removeDir("/my/nice/path"); If you see the directory already empty, try to check for hidden files and be sure that you have the right permissions.

0

I suspect you have already checked its not a file permissions issue. As your code works for me but not you, it makes me wonder if it is a to do with PHP file stat or Real Path caching.

Unlinking a file should clear stat cache for individual file automatically. However PHP bugs have previously been known to cause this issue with rmdir.

Try doing a clearstatcache after the rmdir statement in your foreach block.

Previously I've used glob (mentioned in other answers) so I've no idea how RecursiveDirectoryIterator works re file handles; as a long shot try destroying these objects ( unset($files); unset($it) ) before your final rmdir.

scytale
  • 1,339
  • 1
  • 11
  • 14