PHP rmdir use recursive functions to delete non-empty directory instances detailed _php tips

Source: Internet
Author: User

First, let's introduce the rmdir () function.

PHP rmdir () function

rmdir-Delete Empty Directory

Grammar:

BOOL RmDir (String $dirname [, Resource $context])

An attempt was made to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Failure can result in a e_warning level error.
Parameters:

1.dirname: The path to the directory.
2.context: Added support for context in PHP 5.0.0.

PHP rmdir () Delete non-empty directory

As mentioned above, the RmDir () function can only delete empty directories, if the Non-empty directory needs to enter the directory first, use the unlink () function to delete every file in the directory, and then come back to delete this empty directory. If subdirectories also exist in the directory, and the directory is not empty, you need to use a recursive method. Custom recursive function deletes the source code of the directory as follows:

<?php function
Deldir ($directory) {//Custom function recursive functions the entire directory if
  (file_exists ($directory)) {//Determine whether the directory exists, if no rmdir () function can error
    if ($dir _handle= @opendir ($directory)) {//Open Directory return directory resource and determine if success while
      ($filename =readdir ($dir _handle) {//Traverse directory, read out files or folders in directory
        if ($filename!= '. ' && $filename!= '. ') {//must exclude two special directory
          $subFile = $directory. " /". $filename;//Connect the files in the directory to the current directory if
          (Is_dir ($subFile)) {//Deldir ($subFile) If it is a directory condition
            ;//recursively call yourself to delete the subdirectory
          }
          if (Is_file ($subFile)) {//If a file condition is set up
            unlink ($subFile);//delete this file directly}}
      closedir ($ Dir_handle);//Close Directory Resource
      rmdir ($directory);//Delete Empty Directory}}}

deldir ("Mydir");//Call Deldir function

?>

Handling recursive deletion of non-empty directories, we can also use the operating system command "RM-RF" to remove Non-empty directories, but also from the security and cross-platform considerations as far as possible not to use.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.