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

January 2017

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.


Published by netty5. Latest update on May 14, 2013 at 05:52 AM by deri58.
This document, titled "PHP - Warning: rmdir(..): Directory not empty," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (