How to delete folders, subfolders and files?

Asked

Viewed 10,821 times

7

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

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

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

However, by using the same example to delete a folder that contains 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);
   }
 }

Then you see that $destino and $destino2 are completely different locations, being the last one, which is causing me problems at the time of being removed. Here is an example of what I want to remove through $destino2:

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

How should I proceed?

3 answers

15


In the PHP manual has a example recursive function that does this (despite using ternary operator in a way I don’t 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 it like this:

delTree('../img/uploads/hoteis/3/');

4

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);

1

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

Under 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 recursiveness in PHP functions, because it is the operating system that will do the job.

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

Note that the above example is for teaching purposes and the commands vary according to the Operating System and its respective distributions.

  • Although it’s more performative, I don’t recommend using functions like exec() or eval() in simple cases, for safety reasons.

  • Concern for safety independent of the complexity of the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.