- /**
- 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
Attached: rmdir (PHP 4, PHP 5) rmdir-Delete directory report a bug description bool RmDir (string $dirname) attempt to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Returns TRUE on success, or FALSE on failure. Note: from PHP 5.0.0 rmdir () can also be used for certain URL encapsulation protocols. See the list of supported protocols and Wrappers to see what URL encapsulation protocols are supported by RmDir (). Note: Added support for context in PHP 5.0.0. See the Stream function for a description of the context. Note: When you enable Safe mode, PHP checks that the directory in which the script is executed will have the same UID (owner) as the script being executed when executing the script. 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 was a function to delete a folder, all sub-folders, and the files in one clean move. Just Tell it how directory you want deleted, and the relation to the page and this function is executed. Then Set $empty = True if you want the folder just emptied, and not deleted. If you set $empty = False, or just simply leave it out, the given directory is 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;
- }
- }
- ?
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.
>>>
|