PHP - Warning: rmdir(..): Directory not empty

Ask a question
PHP is the scripting language that is used to create web pages that are dynamic and interactive. PHP has built-in commands that can be used to do a variety of work. One such example is the rmdir-directory command which is used to remove a directory. If the directory is not empty then the developer would run into an error which says 'Directory not empty'. To get rid of this error, one can write a custom code which can scan through all the folders and subfolders recursively and the rmdir-directory command ensures that all the directories are removed without running into any issues.


You are attempting to delete a PHP file and you are getting the error message:

Warning: rmdir(..): Directory not empty . 


This indicates that the directory is not empty. To erase the content of this directory (including subfolders, folders and files), there is a special PHP code that can be used.

The code


        rmdir_recursive function ($ dir)    
         / / List the contents of the directory table    
         $ Dir_content = scandir ($ dir);    
              / / Is it a directory?    
                  If ($ dir_content! == FALSE) {   
                       / / For each directory entry    
                             Foreach ($ dir_content as $ entry)    
                                 / / Unix symbolic shortcuts, we go    
                                  If (! In_array ($ entry, array ('.','..'))){    
                                / / We find the path from the beginning    
                                $ Entry = $ dir. '/'. $ entry;    
                       / / This entry is not an issue: it clears    
                 If (! Is_dir ($ entry)) (    
         Unlink ($ entry);    
    / / This entry is a folder, it again on this issue    
   Else (    
  Rmdir_recursive ($ entry);    
/ / It has erased all entries in the folder, we can now delete ut   
Rmdir ($ dir);    

Note that:

If this function fails, e.g in the case you do not have permission to delete a file, you will see an error message on the web page that may unnecessarily provide information on the content of your directories. This is why it is best to precede the call to this function with an at sign (@), to prevent the display error messages.
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team