Php implementation code for deleting and copying folders and all files in their folders

Source: Internet
Author: User
Recently writing code requires the function of copying folders and all files in these folders. you can use this function to delete these files. you can use the custom functions of xcopy and deldir, it is much more convenient. The code is as follows:
/* Copy the xCopy function usage:
* XCopy ("feiy", "feiy2", 1): copy the files in feiy to feiy2, including subdirectories.
* XCopy ("feiy", "feiy2", 0): copy the file under feiy to feiy2, excluding subdirectories.
* Parameter description:
* $ Source: source directory name
* $ Destination: target directory name
* $ Child: indicates whether to include sub-directories during replication.
*/
Function xCopy ($ source, $ destination, $ child ){
If (! File_exists ($ destination ))
{
If (! Mkdir (rtrim ($ destination, '/'), 0777 ))
{
// $ Err-> add ($ _ LANG ['cannt _ mk_dir ']);
Return false;
}
@ Chmod ($ destination, 0777 );
}
If (! Is_dir ($ source )){
Return 0;
}
If (! Is_dir ($ destination )){
Mkdir ($ destination, 0777 );
}
$ Handle = dir ($ source );
While ($ entry = $ handle-> read ()){
If ($ entry! = ".") & ($ Entry! = "..")){
If (is_dir ($ source. "/". $ entry )){
If ($ child)
XCopy ($ source. "/". $ entry, $ destination. "/". $ entry, $ child );
}
Else {
Copy ($ source. "/". $ entry, $ destination. "/". $ entry );
}
}
}
Return 1;
}


/* Delete deldir function usage:
* Deldidr ("feiy"): deletes feiy, including subdirectories.
* Parameter description:
* $ Dir: name of the directory to be deleted
*/
Function deldir ($ dir ){
If (! File_exists ($ dir) {return true;
} Else {@ chmod ($ dir, 0777 );}
$ Dh = opendir ($ dir );
While ($ file = readdir ($ dh )){
If ($ file! = "." & $ File! = ".."){
$ Fullpath = $ dir. "/". $ file;
If (! Is_dir ($ fullpath )){
Unlink ($ fullpath );
} Else {
Deldir ($ fullpath );
}
}
}

Closedir ($ dh );

If (rmdir ($ dir )){
Return true;
} Else {
Return false;
}
}
?>

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.