search for in the  
<rewindset_file_buffer>
Last updated: Thu, 19 May 2005

rmdir

(PHP 3, PHP 4, PHP 5)

rmdir -- Removes directory

Description

bool rmdir ( string dirname [, resource context] )

Attempts to remove the directory named by dirname. The directory must be empty, and the relevant permissions must permit this. Returns TRUE on success or FALSE on failure.

Note: As of PHP 5.0.0 rmdir() can also be used with some URL wrappers. Refer to Appendix L for a listing of which wrappers support rmdir().

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CXXII, Stream Functions.

Note: When safe mode is enabled, PHP checks whether the directory in which you are about to operate has the same UID (owner) as the script that is being executed.

See also mkdir() and unlink().



User Contributed Notes
rmdir
development at lab-9 dot com
28-Apr-2005 11:35
Here a small and useful function for removing files by deleting also subdirectories recursive.

this function takes:
$target .. a absolute target path, f.e: /var/www/html/remove_this
$exceptions .. a array of directorynames which don't have to be removed .. usually unused as you want to delete whole directory :-)
$output=true .. outputs a status message of which file or directory the script just has accessed to;

<?php
function delete_files($target, $exceptions, $output=true)
{
  
$sourcedir = opendir($target);
   while(
false !== ($filename = readdir($sourcedir)))
   {
       if(!
in_array($filename, $exceptions))
       {
           if(
$output)
           { echo
"Processing: ".$target."/".$filename."<br>"; }
           if(
is_dir($target."/".$filename))
           {
              
// recurse subdirectory; call of function recursive
              
delete_files($target."/".$filename, $exceptions);
           }
           else if(
is_file($target."/".$filename))
           {
              
// unlink file
              
unlink($target."/".$filename);
           }
       }
   }
  
closedir($sourcedir);
   if(
rmdir($target))
   { return
true; }
   else
   { return
false; }
}
?>

here a example of function call:
<?php
$exceptions
= array(".", "..");
if(
delete_files("/var/www/html/this_dir", $exceptions, true))
{ echo
"deletion successed"; }
else
{ echo
"deletion failed"; }
?>

KNOWN ISSUE:
if you call the function with an exception folder and that folder is a childfolder, this function won't be able to remove the parent directory and returns false .. :-/ .. if someone knows a workaround please email me :-)
czambran at gmail dot com
06-Mar-2005 01:21
A quick note on the function posted by Daniellehr[-at-]gmx[-dot-]de, the function as it is will delete everything inside the directory but the directory. The problem is that the function does not close the directory before executing the rmdir line. Here is the corrected function:
function delDir($dirName) {
   if(empty($dirName)) {
       return true;
   }
   if(file_exists($dirName)) {
       $dir = dir($dirName);
       while($file = $dir->read()) {
           if($file != '.' && $file != '..') {
               if(is_dir($dirName.'/'.$file)) {
                   delDir($dirName.'/'.$file);
               } else {
                   @unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
               }
           }
       }
       $dir->close();
       @rmdir($dirName) or die('Folder '.$dirName.' couldn\'t be deleted!');
   } else {
       return false;
   }
   return true;
}
itsdapead
28-Feb-2005 11:03
Warning!  On Unix-type systems, most of the "delete directory tree" functions posted here will follow & delete symbolic links (which is not what you'd expect of, say, rm-rf) so you might find more than you bargained for getting wiped.

Here's a quick kludge of an earlier submission (by tekangel) that avoids this by default.

Make sure it does what you want before you uncomment the unlink and rmdir commands!

<?
function rmdirRecursive($path,$followLinks=false) {
  
  
$dir = opendir($path) ;
   while (
$entry = readdir($dir) ) {
      
       if (
is_file( "$path/$entry" ) || ((!$followLinks) && is_link("$path/$entry")) ) {
           echo (
"unlink $path/$entry;\n" );
          
// Uncomment when happy!
           //unlink( "$path/$entry" );
      
} elseif ( is_dir( "$path/$entry" ) && $entry!='.' && $entry!='..' ) {
          
rmdirRecursive( "$path/$entry" ) ;
       }
   }
  
closedir($dir) ;
   echo
"rmdir $path;\n";
  
// Uncomment when happy!
   // return rmdir($path);
}
?>
Daniellehr[-at-]gmx[-dot-]de
20-Feb-2005 03:55
If you want to delete a folder with its all content and sub-content you can use this recursive-working function
(WARNING: after using the function the folder is completely deleted!):
<?php
function delDir($dirName) {
   if(empty(
$dirName)) {
       return;
   }
   if(
file_exists($dirName)) {
      
$dir = dir($dirName);
       while(
$file = $dir->read()) {
           if(
$file != '.' && $file != '..') {
               if(
is_dir($dirName.'/'.$file)) {
                  
delDir($dirName.'/'.$file);
               } else {
                   @
unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
               }
           }
       }
       @
rmdir($dirName.'/'.$file) or die('Folder '.$dirName.'/'.$file.' couldn\'t be deleted!');
   } else {
       echo
'Folder "<b>'.$dirName.'</b>" doesn\'t exist.';
   }
}
?>
aidan at php dot net
05-Sep-2004 02:48
If you want to delete a file, or an entire folder (including the contents), use the below function.

http://aidan.dotgeek.org/lib/?file=function.rmdirr.php

<rewindset_file_buffer>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT