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]
rmdir
(PHP 4, PHP 5)
rmdir — Efface un dossier
Description
bool rmdir
( string $dirname
[, resource $context
] )
Tente d'effacer le dossier dont le chemin est dirname . Le dossier doit être vide, et le script doit avoir les autorisations adéquates.
Liste de paramètres
- dirname
-
Le chemin vers le dossier.
- context
-
Note: Le support de contexte a été ajouté en PHP 5.0.0. Pour une description des contextes, référez-vous à Fonctions sur les flux.
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Historique
| Version | Description |
|---|---|
| 5.0.0 | Depuis PHP 5.0.0, rmdir() peut aussi être utilisée avec certains gestionnaires d'URL. Reportez vous à Liste des protocoles supportés pour une liste des gestionnaires qui supportent rmdir(). |
Exemples
Exemple #1 Exemple avec rmdir()
<?php
if (!is_dir('examples')) {
mkdir('examples');
}
rmdir('examples');
?>
Notes
Note: Lorsque le safe-mode est activé, PHP vérifie si le fichier/dossier que vous allez utiliser a le même UID que le script qui est actuellement exécuté.
rmdir
asn at asn24 dot dk
07-Jul-2009 04:08
07-Jul-2009 04:08
senthryl at NOSPAM dot geemail dot com
25-Jun-2009 06:39
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
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
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
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
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
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
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
