downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

set_file_buffer> <rewind
Last updated: Fri, 13 Nov 2009

view this page in

rmdir

(PHP 4, PHP 5)

rmdirディレクトリを削除する

説明

bool rmdir ( string $dirname [, resource $context ] )

dirname で指定されたディレクトリを 削除しようと試みます。ディレクトリは空でなくてはならず、また 適切なパーミッションが設定されていなければなりません。

パラメータ

dirname

ディレクトリへのパス。

context

注意: コンテキストのサポートは、 PHP 5.0.0 で追加されました。contexts の説明に関しては、 ストリーム 関数 を参照してください。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

変更履歴

バージョン 説明
5.0.0 PHP 5.0.0 以降、rmdir()いくつかの URL ラッパーを併用することが可能です。 rmdir() をサポートしているラッパーの一覧については、 サポートするプロトコル/ラッパー を参照ください。

例1 rmdir() の例

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

rmdir('examples');
?>

注意

注意: セーフモード が有効の場合、PHP は、 操作を行うディレクトリが、実行するスクリプトと同じ UID (所有者)を有しているか どうかを確認します。

参考

  • is_dir() - ファイルがディレクトリかどうかを調べる
  • mkdir() - ディレクトリを作る
  • unlink() - ファイルを削除する



set_file_buffer> <rewind
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
rmdir
omikrosys at gmail dot com
09-Oct-2009 04: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 12: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 04: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 04: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]
senthryl at NOSPAM dot geemail dot com
25-Jun-2009 06:39
Here's another version of the recursive directory removal.  This version requires PHP 5, and returns TRUE on success or FALSE on failure.

<?php
   
function deleteDirectory($dir) {
        if (!
file_exists($dir)) return true;
        if (!
is_dir($dir)) return unlink($dir);
        foreach (
scandir($dir) as $item) {
            if (
$item == '.' || $item == '..') continue;
            if (!
deleteDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
        }
        return
rmdir($dir);
    }
?>
j_terlouw at planet dot nl
17-Jun-2009 08:24
This function deletes a folder and all it's subfolders and files.

<?php
   
   
function remove_dir($current_dir) {
   
        if(
$dir = @opendir($current_dir)) {
            while ((
$f = readdir($dir)) !== false) {
                if(
$f > '0' and filetype($current_dir.$f) == "file") {
                   
unlink($current_dir.$f);
                } elseif(
$f > '0' and filetype($current_dir.$f) == "dir") {
                   
remove_dir($current_dir.$f."\\");
                }
            }
           
closedir($dir);
           
rmdir($current_dir);
        }
    }

?>

Might need some improvement, but it works fine.
maurozadu at gmail dot com
16-Jun-2009 03: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 10: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.
not at any dot com
14-Apr-2006 06:21
Save some time, if you want to clean a directory or delete it and you're on windows.

Use This:

            chdir ($file_system_path);
            exec ("del *.* /s /q");

You can use other DEL syntax, or any other shell util.
You may have to allow the service to interact with the desktop, as that's my current setting and I'm not changing it to test this.
aidan at php dot net
05-Sep-2004 07:48
If you want to delete a file, or an entire folder (including the contents), use the below function.

http://aidanlister.com/repos/v/function.rmdirr.php

set_file_buffer> <rewind
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites