PHP recursive delete directory and file custom function Rrmdir

Source: Internet
Author: User
    1. /**
    2. Recursively delete a directory and all files under it
    3. Func:rrmdir
    4. */
    5. function Rrmdir ($dir) {
    6. if (Is_dir ($dir)) {
    7. $objects = Scandir ($dir);
    8. foreach ($objects as $object) {
    9. if ($object! = "." && $object! = "...") {
    10. if (filetype ($dir. " /". $object) = =" dir ") Rrmdir ($dir." /". $object); Else unlink ($dir. " /". $object);
    11. }
    12. }
    13. Reset ($objects);
    14. }
    15. }
    16. ?>
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 ().

    1. function Rrmdir ($dir) {
    2. if (Is_dir ($dir)) {
    3. $objects = Scandir ($dir);
    4. foreach ($objects as $object) {
    5. if ($object! = "." && $object! = "...") {
    6. if (filetype ($dir. " /". $object) = =" dir ") Rrmdir ($dir." /". $object); Else unlink ($dir. " /". $object);
    7. }
    8. }
    9. Reset ($objects);
    10. RmDir ($dir);
    11. }
    12. }
    13. ?>
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.

    1. !--? php function deleteAll ($directory, $empty = False) {
    2. if (substr ($directory,-1) = = "/") {
    3. $directory = substr ($directory, 0,-1);
    4. }
    5. if (!file_exists ($directory) | |!is_dir ($directory)) {
    6. return false;
    7. } elseif (!is_readable ($directory)) {
    8. return false;
    9. } else {
    10. $directoryHandle = Opendir ($directory);
    11. while ($contents = Readdir ($directoryHandle)) {
    12. if ($contents! = '. ' && $contents! = ' ... ') {
    13. $path = $directory. "/" . $contents;
    14. if (Is_dir ($path)) {
    15. deleteAll ($path);
    16. } else {
    17. unlink ($path);
    18. }
    19. }
    20. }
    21. Closedir ($directoryHandle);
    22. if ($empty = = False) {
    23. if (!rmdir ($directory)) {
    24. return false;
    25. }
    26. }
    27. return true;
    28. }
    29. }
    30. ?
Copy Code

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

    1. Delete folder function
    2. function DeleteDirectory ($dir) {
    3. if (!file_exists ($dir)) return true;
    4. if (!is_dir ($dir) | | is_link ($DIR)) return unlink ($dir);
    5. foreach (Scandir ($dir) as $item) {
    6. if ($item = = '. ' | | $item = = ' ... ') continue;
    7. if (!deletedirectory ($dir. "/" . $item)) {
    8. chmod ($dir. "/" . $item, 0777);
    9. if (!deletedirectory ($dir. "/" . $item)) return false;
    10. };
    11. }
    12. return rmdir ($dir);
    13. }
    14. ?>
Copy Code

For more information, please refer to Http://cn.php.net/rmdir.

>>>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.