How to delete folders, subfolders and files?

7

Using my example below, I can delete a folder and the files it contains:

 $uploaddir = "../img/uploads/".$destino_sa."/";
 $dir_contents = scandir($uploaddir);

 if(is_dir($uploaddir)) {
   foreach($dir_contents as $content) {
     unlink($uploaddir.'/'.$content);
     rmdir($uploaddir);
   }
 }

However, when using the same example to delete a folder containing a subfolder and its files, I am not successful:

$uploaddir2 = "../img/uploads/hoteis/".$id_destino2."/".$destino_sa2."/";
$dir_contents2 = scandir($uploaddir);

 if(is_dir($uploaddir2)) {
   foreach($dir_contents2 as $content2) {
     unlink($uploaddir2.'/'.$content2);
     rmdir($uploaddir2);
   }
 }

It will then be seen that $destino and $destino2 are completely different places, the last being that is causing me problems at the time of being removed. Here's an example of what I want to remove through $destino2 :

../img/uploads/hoteis/3/hotel_emiliano/
  • hotel_emiliano folder and its contents
  • 3 folder and its contents, in this case the hotel_emiliano

How do I proceed?

    
asked by anonymous 08.05.2014 / 04:14

3 answers

14

In the PHP manual has a example of recursive function that does this (although using the ternary operator in a way that I do not like ):

public static function delTree($dir) { 
  $files = array_diff(scandir($dir), array('.','..')); 
  foreach ($files as $file) { 
    (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); 
  } 
  return rmdir($dir); 
}

In your case, you can use this way:

delTree('../img/uploads/hoteis/3/');
    
08.05.2014 / 04:54
3

You can also use SPL Iterators Class with RecursiveIteratorIterator and RecursiveDirectoryIterator available from PHP 5.3.0, see:

 $directory = 'hotel_emiliano';

 foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory,FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $file){
    $file->isFile() ? unlink($file->getPathname()) : rmdir($file->getPathname());
 }
 rmdir($directory);
    
08.05.2014 / 19:20
1

Alternatively, the responses posted, you can use the Shell functions.

In Linux environment, to remove a directory and all its contents:

ls | xargs rm -rf

In Windows environment

RMDIR \"drive_letter:\path\of\directory\" /S /Q

In PHP, under Linux, it would look like this:

// Caminho do diretório
$path = '/path/of/directory/';
// Muda para o diretório
chdir($path);
// Executa o comando sob o diretório
exec('ls | xargs rm -rf');
// Apaga a pasta que deve estar vazia, nesse ponto.
rmdir($path);

Under Windows

exec('RMDIR \"drive_letter:\path\of\directory\" /S /Q');

This method is more performative than using recursion in PHP functions, as it is the operating system that will do the work.

Obviously, you need the appropriate permissions for such an operation. You also need to check if chdir () returns false or true before proceeding with exec ().

Note that the example above is for didactic purposes and commands vary depending on the Operating System and its distributions.

    
08.05.2014 / 20:49