How to Delete Directory with Files in php laravel ?

In This article will provide some of the most important example how to delete directory with files in php laravel. step by step explain php remove dir and all contents. You can succes delete directory with files in laravel php.

Sometime, we need to delete all files and directory in a directory using php code. so in this post i will help you how to remove folder with all contents inside that folder yahh this is reqire.

Let’s see both example that will helps you.

Example 1:

Code:

<?php
  
$folderName = 'images';
  
deleteFolder($folderName);
   
function deleteFolder($folderName) {
         if (is_dir($folderName))
           $folderHandle = opendir($folderName);
  
         if (!$folderHandle)
              return false;
  
         while($file = readdir($folderHandle)) {
               if ($file != "." && $file != "..") {
                    if (!is_dir($folderName."/".$file))
                         unlink($folderName."/".$file);
                    else
                         deleteFolder($folderName.'/'.$file);
               }
         }
  
         closedir($folderHandle);
         rmdir($folderName);
         return true;
}
  
?>

Example 2:

Code:

<?php
  
$folderName = 'files';
  
deleteFiles($folderName);
   
function deleteFiles($target) {
    if(is_dir($target)){
        $files = glob( $target . '*', GLOB_MARK );
  
        foreach( $files as $file ){
            deleteFiles( $file );      
        }
  
        rmdir( $target );
    } elseif(is_file($target)) {
        unlink( $target );  
    }
}
  
?>

I hope it can help you…

Leave a Comment