chessweb
chessweb

Reputation: 4645

rmdir in PHP not working on an empty directory

I'm trying to remove a directory with PHP.

I unlink/remove all files/subdirs from the inside out and finally call rmdir on the now empty top directory. Everything goes according to plan until the last call to rmdir. PHP warns that the directory is NOT emtpy and refuses to remove it. But when I look at the directory in the explorer it is empty, after all.

I also tried a well known recursive function with the same result.

The operating system is Windows 7 with Xampp and there are no access restrictions for any of the elements in question.

Any ideas?

Upvotes: 4

Views: 7406

Answers (2)

Malek Tubaisaht
Malek Tubaisaht

Reputation: 1387

function rrmdir($dir) {
   if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
   }
}

Upvotes: 1

Ahmet Kakıcı
Ahmet Kakıcı

Reputation: 6404

Can you try this one?

<?php
$handle = opendir($dirpath);
//do whatever you need
closedir($handle)
rmdir($dirpath);
?>

Upvotes: 7

Related Questions