PHP Create and delete directory functions introduction and recursive delete directory function sharing _php tips

Source: Internet
Author: User
Tags mkdir phpmyadmin

mkdir ()-New directory

Copy Code code as follows:

– Syntax: bool mkdir (String pathname [, int mode])
– Try a new directory specified by pathname.

rmdir ()-Delete directory
Copy Code code as follows:

– Syntax: bool RmDir (String dirname)
– Try to delete the directory specified by the dirname. The directory must be empty and have the appropriate permissions. Returns TRUE if successful, or returns if failed
FALSE.

unlink-Delete Files
Copy Code code as follows:

– Syntax: BOOL Unlink (string filename)
– Delete filename. Similar to the unlink () function of Unix C. Returns TRUE if successful, and returns FALSE if it fails.

In PHP, you can easily create a new directory by using the mkdir () function only to pass in a directory name. However, the function used to delete the directory rmdir (), can only delete an empty directory and the directory must exist. If the Non-empty directory needs to go into the directory first, use the unlink () function to delete every file in the directory, and then come back and delete the empty directory. If the directory still exists in the directory, and the subdirectory is not empty, you will use a recursive method. Custom recursive functions The program code for deleting a directory looks like this:

Copy Code code as follows:

<?php
Custom function recursively delete entire directory
function Deldir ($directory) {
if (file_exists ($directory)) {//if rmdir () function does not exist there will be an error
if ($dir _handle = @opendir ($directory)) {//Open directory and determine if it will open successfully
while ($filename = Readdir ($dir _handle)) {//Loop through all the files in the directory
if ($filename!= "." && $filename!= "...") {//Be sure to exclude two special directories
$subFile = $directory. "       /". $filename; Connect the files in the directory to the current directory
if (Is_dir ($subFile))//If the directory is the condition
Deldir ($subFile); Recursively call itself functions, delete subdirectories
if (Is_file ($subFile))//If the file is the condition
Unlink ($subFile); Delete this file directly
}
}
Closedir ($dir _handle); To close a file resource
RmDir ($directory); Delete Empty Directory
}
}
}

Dirdir ("phpMyAdmin"); Call the Deldir () function to delete the "phpMyAdmin" file in the directory where the program resides
?>

Of course, you can also delete a non-empty directory by calling the operating system command "RM-RF", but consider not using it from security and cross-platform considerations as well.

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.