-4

Possible Duplicate:
PHP: delete directory with files in it?

If i've some files inside the following path

mysite.com/install

and i've created file named as "die.php" outside /install/ where its path mysite.com/die.php once i executed it, it should delete install folder with all its files inside.

I've added to die.php the following code

<?PHP rmdir('install');?>

but it only delete /install/ folder when it is empty ! and if it have files it gives error Directory not empty in mysite.com/install

so any idea how to do it.

Community
  • 1
  • 1
Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71
  • Please use the search function before asking questions. This question has been asked and answered before and is also easy to google for. We don't want this place to be littered with duplicates. Thanks. – Gordon Oct 28 '12 at 19:05

2 Answers2

3

If you're looking for a pure PHP solution, you will have to loop though all subfolders (see answer just posted above). For a faster approch that only works on a platform with GNU coreutils (any major Linux distribution), use this:

system('rm -rf '.$dir);

A word of advice: This is potentially unsafe (possible to pipe a command) - be very sure to sanitize your input.

Aurelia
  • 1,052
  • 6
  • 28
1

This should work

<?php
 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);
   }
 }
?>

I found it on the php site. This seems to be a common issue.

Andy D
  • 896
  • 7
  • 12