Filesystem 函数
在线手册:中文 英文
PHP手册

rmdir

(PHP 4, PHP 5)

rmdir删除目录

说明

bool rmdir ( string $dirname )

尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。成功时返回 TRUE, 或者在失败时返回 FALSE.

Note: 自 PHP 5.0.0 起 rmdir() 也可用于某些 URL 封装协议。参见Supported Protocols and Wrappers 的列表看看 rmdir() 支持哪些 URL 封装协议。

Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。 有关 上下文(Context) 的说明参见 Streams

Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

参见 mkdir()unlink()

参数

dirname

Path to the directory.

context

Note: 在 PHP 5.0.0 中增加了 对上下文(Context)的支持。 有关 上下文(Context) 的说明参见 Streams

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE.

更新日志

版本 说明
5.0.0 As of PHP 5.0.0 rmdir() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support rmdir().

范例

Example #1 rmdir() example

<?php
if (!is_dir('examples')) {
    
mkdir('examples');
}

rmdir('examples');
?>

注释

Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

参见


Filesystem 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 删除目录

用户评论:

longears at BLERG dot gmail dot com (30-Mar-2012 03:10)

Concise way to recursively remove a directory:

<?php
# recursively remove a directory
function rrmdir($dir) {
    foreach(
glob($dir . '/*') as $file) {
        if(
is_dir($file))
           
rrmdir($file);
        else
           
unlink($file);
    }
   
rmdir($dir);
}
?>

Anonymous (23-Jan-2012 11:20)

When the directory is not empty:

<?php
 
function rrmdir($dir) {
   if (
is_dir($dir)) {
    
$objects = scandir($dir);
     foreach (
$objects as $object) {
       if (
$object != "." && $object != "..") {
         if (
filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
    
reset($objects);
    
rmdir($dir);
   }
 }
?>

thomas (21-Nov-2011 08:44)

Function deleteAll given by O S on 18-Jun-2010 11:30 will fail at line

while ($contents = readdir($directoryHandle)) {...

if a folder named 0 (zero) is found during traversing the hierarchy

shane dot ray87 at gmail dot com (08-Aug-2011 09:50)

This issue has been driving me nuts for hours.

I am running PHP on IIS, I had the wincache module installed, when running a recursive delete a certain folder would get "stuck" and throw permissions errors.  I was not able to delete them with PHP or in windows itself.  The only way to delete the folder was to wait 5 min and run the script again, or stop the IIS server and the folder would delete on its own.  Disabling the wincachce module resolved the issue.

Hope this helps.

samy dot see at gmail dot com (27-Jun-2011 11:38)

if you get this problem Permission denied in windows testing your site maybe this will resolve the problem

<?php
if(file_exists($path.'/Thumbs.db')){
   
unlink($path.'/Thumbs.db');
}
?>

and then

<?php rmdir($path); ?>

holger1 at NOSPAMzentralplan dot de (26-Jun-2010 10:00)

Another simple way to recursively delete a directory that is not empty:

<?php
 
function rrmdir($dir) {
   if (
is_dir($dir)) {
    
$objects = scandir($dir);
     foreach (
$objects as $object) {
       if (
$object != "." && $object != "..") {
         if (
filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
    
reset($objects);
    
rmdir($dir);
   }
 }
?>

O S (18-Jun-2010 11:30)

This isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.

Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.

<?php
function deleteAll($directory, $empty = false) {
    if(
substr($directory,-1) == "/") {
       
$directory = substr($directory,0,-1);
    }

    if(!
file_exists($directory) || !is_dir($directory)) {
        return
false;
    } elseif(!
is_readable($directory)) {
        return
false;
    } else {
       
$directoryHandle = opendir($directory);
       
        while (
$contents = readdir($directoryHandle)) {
            if(
$contents != '.' && $contents != '..') {
               
$path = $directory . "/" . $contents;
               
                if(
is_dir($path)) {
                   
deleteAll($path);
                } else {
                   
unlink($path);
                }
            }
        }
       
       
closedir($directoryHandle);

        if(
$empty == false) {
            if(!
rmdir($directory)) {
                return
false;
            }
        }
       
        return
true;
    }
}
?>

info at top-info dot org (28-Apr-2010 12:15)

The function delTree is dangerous when you dont take really care. I for example always deleted a temporary directory with it. Everthing went fine until the moment where the var containing this temporary directory wasnt set. The var didnt contain the path but an empty string. The function delTree  was called and deleted all the files at my host!
So dont use this function when you dont have a proper handling coded. Dont think about using this function only for testing without such a handling.
Luckily nothing is lost because I had the local copy...

steffen at wirsching-idstein dot de (17-Jan-2010 01:40)

Say, you're working on Windows and continue to get a permission's error without a reason. Then it may be that a different Windows program is working on the folder (see earlier notes also). In the case that you can't find that program, the line

<?php closedir(opendir($dirname)); ?>

may solve the problem!
Make sure to write this before rmdir($dirname);.

omikrosys at gmail dot com (09-Oct-2009 05:52)

Sometimes you would face situations in which rmdir($dirname) would give "permission denied" errors though you may have changed $dirname permissions. In such situations just change the permissions of the directory which contains $dirname and rmdir($dirname) would work like a charm.
Say you use rmdir('dirr'); then change the permissions of the folder that contains 'dirr'.

kevin at web-power dot co dot uk (02-Oct-2009 01:51)

I had situation where the rmdir was returning warning message as within last loop it was already removed. So here is quick fix by adding is_dir to the DelTree routine below

<?php
function delTree($dir) {
   
$files = glob( $dir . '*', GLOB_MARK );
    foreach(
$files as $file ){
        if(
substr( $file, -1 ) == '/' )
           
delTree( $file );
        else
           
unlink( $file );
    }
   
    if (
is_dir($dir)) rmdir( $dir );
   
}
?>

bcairns at gmail dot com (03-Aug-2009 05:59)

I wasn't having much luck with the recursive delete functions below, so I wrote my own:

<?php
// ensure $dir ends with a slash
function delTree($dir) {
   
$files = glob( $dir . '*', GLOB_MARK );
    foreach(
$files as $file ){
        if(
substr( $file, -1 ) == '/' )
           
delTree( $file );
        else
           
unlink( $file );
    }
   
rmdir( $dir );
}
?>

Simple.  Works.

asn at asn24 dot dk (07-Jul-2009 05:08)

A patch to previous script to make sure rights for deletion is set:

<?php
//Delete folder function
function deleteDirectory($dir) {
    if (!
file_exists($dir)) return true;
    if (!
is_dir($dir) || is_link($dir)) return unlink($dir);
        foreach (
scandir($dir) as $item) {
            if (
$item == '.' || $item == '..') continue;
            if (!
deleteDirectory($dir . "/" . $item)) {
               
chmod($dir . "/" . $item, 0777);
                if (!
deleteDirectory($dir . "/" . $item)) return false;
            };
        }
        return
rmdir($dir);
    }
?>

[EDITOR NOTE: "Credits to erkethan at free dot fr." - thiago]

maurozadu at gmail dot com (16-Jun-2009 04:03)

if you opened a dir with opendir() you must closedir() before try to execute rmdir() or you will get a "permision denied" error on windows systems.

Wrong:

<?php
$handle
= opendir($dirpath);
//do whatever you need
rmdir($dirpath);
?>

Right:

<?php
$handle
= opendir($dirpath);
//do whatever you need
closedir($handle)
rmdir($dirpath);
?>

TrashF at taistelumarsu dot org (08-Aug-2008 11:21)

In case you're trying to rmdir() and you keep getting 'Permission denied' errors, make sure you don't have the directory still open after using opendir(). Especially when writing recursive functions for deleting directories, make sure you have closedir() BEFORE rmdir().

rn at clubfl dot com (18-Dec-2007 06:16)

I've noticed that when using this command on a windows platform you may encounter a permissions error which may seem unwarranted. This commonly occurs if you are or were using a program to edit something in the to be deleted folder and either the item is still in the folder or the program that was accessing the file in that folder is still running(causing it to hold onto the folder).

SO... if you get a permissions error and there shouldn't be an issue with folder permissions check if there are files in there then check if there is a program running that is or was using a file that was in that folder and kill it.