I was working on my PHP Tutorial and I was working on file management functions in PHP. PHP has a function called rmdir() that will remove an empty directory for you but it can not delete a non-empty folder. I was board so I just wrote this and tested it on my server It will recursively go into each sub-directory delete all the files and folders hope it is helpful to you:

/*  Sudo Code for the function
     if there isn’t any dirname mentioned or the name is not a directory {just end the script
     } otherwise{
          get all the files and folders in the directory
          moves into the directory
          for each file and folder do this check{
               as long as it is not . and .. (the standard folders) then do this:{
                    if it is a folder{
                         call this function to delete it
                         delete the file
          we moved into this folder before so now we move back up one level
          delete the folder
function remove_dir($name){
     if($name==”” or !file_exists($name)){
          echo “I need something to delete!<br />”;
     }//—————end of if
     foreach($ars as $ar){
          if($ar!=”.” and $ar!=”..”){
                    echo “found directory $ar <br />”;
                    echo “found a file $ar <br />”;
               }//——————–end of if
          }//——————-end of if
     }//—————-end of foreach
}//—————end of function
remove_dir(Directory name here);// put any directory name here and it will delete it for you

Tell me how to improve it or if you have any recommendations for this code

%d bloggers like this: