1

How do i delete all files and directories in folder one. Below is my folder structure:

folder structure

Delete.php

<?php

    function rrmdir($dir) { 
      $dir = 'C:xampp/htdocs/project/user/one';
       if (is_dir($dir)) {
         $objects = scandir($dir); 
         foreach ($objects as $object) { 
           if ($object != "." && $object != "..") { 
             if (is_dir($dir."/".$object)){
               rrmdir($dir."/".$object);
             }
             else{
               unlink($dir."/".$object); 
             }
           } 
         }
         rmdir($dir); 
       } 
    }

    ?>

I have tried the code that i get from here but the code did not do anything. As if the function is not working.

Community
  • 1
  • 1
  • can you try this one, http://stackoverflow.com/questions/11613840/remove-all-files-folders-and-their-subfolders-with-php – manian May 10 '17 at 09:48
  • 1
    At the start of the function, your setting the directory, so every time you call this function it will try and process the same directory. – Nigel Ren May 10 '17 at 09:56

1 Answers1

0

This will delete your files recursively. It will work fine, and make sure to have a backup of your files, before getting it deleted. Here we are using glob function to delete files recursively.

<?php

ini_set('display_errors', 1);
function delete($filePath,$array=array())
{
    if(is_array($array) && count($array)>0)
    {
        foreach($array as $filePath)
        {
            if(is_dir($filePath))
            {
                delete(glob($filePath."/*"));//first calling function itself to remove files first.
                rmdir($filePath);//removing directory at the end.
            }
            else    
            {
                unlink($filePath);//unlinking a file.
            }   
        }
    }

}
print_r(delete(glob("C:xampp/htdocs/project/user/one/*")));
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42