PHP recursive delete files and directory functions _php tutorial

Source: Internet
Author: User
Delete files and directories in PHP rmdir () function can be done, but to delete a non-empty directory, will not be able to quickly delete, you must first delete the directory files, but there may be subdirectories in the directory, so you want to do PHP recursive delete directory


Recursive functions are self-calling functions, in the body of the function directly or indirectly call themselves, but need to set the self-invocation of the condition, if the condition is satisfied, then call the function itself, if not satisfied then terminate the function of the self-invocation, and then the current process of the main control back to the previous layer function to execute.

Let's take a look at a classic recursive delete function

Function: Delete all files under the directory and delete the directory

Function code:

The code is as follows Copy Code

function Deldir ($dirname) {
if (file_exists ($dirname)) {//first determine if the directory is valid
$dir = Opendir ($dirname);//Open Directory with Opendir
while ($filename = Readdir ($dir)) {//Use Readdir loop to read contents of directory
if ($filename! = "." && $filename! = "...") {//exclude '. ' and ".." These two special directories
$file = $dirname. " /". $filename;
if (Is_dir ($file)) {//determines if it is a directory, and if yes it calls itself
Deldir ($file); Using recursion to delete subdirectories
}else{
@unlink ($file);//delete files
}
}
}
Closedir ($dir);//close file operation handle
RmDir ($dirname);//delete directory
}
}
?>

Use instance: Public is a folder with many folders and files, call Deldir ($dirname) to delete it

$dir = ' public ';//pass in a folder path
Deldir ($dir);//Call function
?>

Note: First determine if public exists, if it is open public, then use Readdir loop to read the contents of the public directory, if there is "." and ".." These two special directories are excluded. If a folder is encountered, it calls itself processing until the condition is not satisfied. The files are deleted directly when they are encountered. The last layer jumps out of the delete public.


Instance two, deleting a non-empty directory

The code is as follows Copy Code

/*
Custom Delete functions that can delete files and recursively delete folders
*/
function My_del ($path)
{
if (Is_dir ($path))
{
$file _list= Scandir ($path);
foreach ($file _list as $file)
{
if ($file! = '. ' && $file! = ' ... ')
{
My_del ($path. ' /'. $file);
}
}
@rmdir ($path); This method does not have to tell if the folder is empty, because the folder is empty at the beginning, when it arrives here, it is empty.
}
Else
{
@unlink ($path); These two places best still have to use @ Shielding warning error, watching the frustration
}

}


$path = ' d:/Technical document-copy '; The folder to delete

If the PHP file is not ANSI, but UTF-8 mode, and the folder you want to delete contains Chinese characters, you need to transcode the function before calling it
$path =iconv (' utf-8 ', ' gb2312 ', $path);

My_del ($path);

Example 3 to see why the failure was successful

code as follows copy code
<? PHP
Functiondeletedir ($dir) {
if (!handle= @opendir ($dir)) {//detects if there is a
die ("no directory") to open the directory;
}
while (false!== ($file =readdir ($handle))) {
if ($file!== ".") && $file!== "..") {//excludes the current directory from the parent directory
$file = $dir. Directory_separator. $file;
if (Is_dir ($file)) {
Deletedir ($file);
}else{
if (@unlink ($file)) {
echo ' file $file Delete succeeded.
";
}else{
echo "file $file Delete failed!"
";
}
}
}
if (@rmdir ($dir)) {
echo ' directory $dir Delete succeeded.
N ";
}else{
Echo "directory $dir Delete failed!
N ";
}
}

//test program
$dir = "/var/www/test";
Deletedir ($dir);
? >


Create a Write folder and file test under the/var/www/test folder
Shell>touchaaa
shell>touchbbb
Shell>touchccc
Shell>toucheee
Shell>touchffff
shell>mkdir111
shell>mkdir222
shell>mkdir333
Write files in the 111,222,333 folder, respectively. There's not much to say, and then give them permission.
Shell>chown[url]www.www[/url]test-r

http://www.bkjia.com/PHPjc/632793.html www.bkjia.com true http://www.bkjia.com/PHPjc/632793.html techarticle Delete files and directories in PHP rmdir () function can be done, but to delete a non-empty directory, will not be able to quickly delete, you must first delete the directory files, but the directory can ...

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