deleting a folder thats not empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

deleting a folder thats not empty

Post by Obadiah »

is something like this possible in php? i know i can delete an empty folder but how do i delete one that has contents?
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

I am not 100% sure that is possible to do in one shot. You might have to read the contents of the dir and unlink each file. Then unlink the entire dir.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You can do this with php 5 (I think 5.1+) and use SPL

Code: Select all

foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator('my/path/') ) as $file )
{
  $file->delete() ;
}
The $file->delete() is probably incorrect as this is from memory, you also have to filter out the '.' and '..', but with SPL you can delete an entire directory tree with about 4 or 5 lines of code similar to the above. A good place to start is http://www.wiki.cc/php and http://www.php.net/spl
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

which operating system?

with linux you can do this easily with

Code: Select all

rm -r /path/to/directory/
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

im using winserver 2003
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

im gonna see if this snippet will work

Code: Select all

<?php

function remove_directory($dir) {
  if ($handle = opendir("$dir")) {
   while (false !== ($item = readdir($handle))) {
     if ($item != "." && $item != "..") {
       if (is_dir("$dir/$item")) {
         remove_directory("$dir/$item");
       } else {
         unlink("$dir/$item");
         echo " removing $dir/$item<br>\n";
       }
     }
   }
   closedir($handle);
   rmdir($dir);
   echo "removing $dir<br>\n";
  }
}

remove_directory("/path/to/dir");

?>
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I'm not sure how you delete files on Windows server but I believe the following code will identify if the directory is empty or not.

NOTE: This works on a Linux system but I'm not sure how many element will be in the $contents array if the directory is empty on a Windows server.



Code: Select all

$dirContents = opendir("directoryPath");

$i = 0;
while ($files = readdir($dirContents)) {
  $contents[$i] = $files;
  $i++;
}

if (count($contents) <= 2) {
  // This will run if the directory is empty.
}
else {
  // This will run if the there are files in the directory
}
If that doesn't work, try using

Code: Select all

print_r($contents);
while the directory IS empty to see how many elements are in the array and then change

Code: Select all

if (count($contents) <= 2) {
accordingly.

Hope that helps,
Post Reply