PHP rmdir using recursive functions to delete non-empty directories

Source: Internet
Author: User
As we all know, the PHP rmdir () function is used to delete empty directories, but if you want to delete a non-empty directory, we must delete the files or subdirectories in the non-empty directory, this article introduces how PHP uses recursive functions to delete non-empty directories, the need for friends can refer to.
First, let me introduce you to 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 results in a e_warning level error.
Parameters:

DirName: The path to the directory.

Context: Added support for contexts in PHP 5.0.0.

PHP rmdir () Delete a non-empty directory

As mentioned above, the RmDir () function can only delete empty directories, if the non-empty directories need to go to the directory first, use the unlink () function to delete each file in the directory, and then come back to delete the empty directory. If subdirectories exist in the directory, and the directory is not empty, you need to use recursive methods. The source code for the custom recursive function delete directory is as follows:

 
  

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

  • 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.