-
- /**
- Recursively delete a directory and all files under it
- func: rrmdir
- */
- 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);
- }
- }
- ?>
Copy code
Attachment: rmdir
(PHP 4, PHP 5)
rmdir — delete a directory
Report a bug Description
bool rmdir ( string $dirname )
Attempts to delete the directory specified by dirname. The directory must be empty and must have appropriate permissions. Returns TRUE on success, or FALSE on failure.
Note: Since PHP 5.0.0 rmdir() can also be used with certain URL wrapping protocols. See the list of Supported Protocols and Wrappers to see which URL wrapping protocols rmdir() supports.
Note: Support for Context was added in PHP 5.0.0. See the Stream function for a description of context.
Note: When safe mode is enabled, PHP will check when executing a script whether the directory being scripted has the same UID (owner) as the script being executed.
See mkdir() and unlink().
-
- 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);
- }
- }
- ?>
Copy code
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.
-
- 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;
- }
- }
- ?>
-
Copy code
A patch to previous script to make sure rights for deletion is set:
-
- //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);
- }
- ?>
Copy code
For more information, please refer to http://cn.php.net/rmdir.
>>>>
|