How to delete the entire directory with in its files in php

Generally we use the remove directory rmdir() method. In this case the directory will be removed when it has no files on it.
another way is unlink() method. This method can be used for removing the single file from the directory location.

How to download the multiple directories as a zip file in PHP

If you want to remove the directory and its all files and sub directories.
First removes the sub directories and its files after that removes the main directory. This is called as the recursion.
 <?php  
  function deleteDir($dirPath) {  
   if (! is_dir($dirPath)) {  
     throw new InvalidArgumentException("$dirPath must be a directory");  
   }  
   if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {  
     $dirPath .= '/';  
   }  
   $files = glob($dirPath . '*', GLOB_MARK);  
   foreach ($files as $file) {  
     if (is_dir($file)) {  
       deleteDir($file);  
     } else {  
       unlink($file);  
     }  
   }  
   rmdir($dirPath);  
 }  
 deleteDir("Directoryname");  
 ?>  

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff


EmoticonEmoticon