Php mobile folder and file program code

Source: Internet
Author: User
Tags parent directory

Method 1: copy + unlink

My idea is: move = new + delete. Create a folder in the target directory before moving the folder, copy the file and directory, and delete the folder.

The code is as follows: Copy code

/**
* @ ParammoveDir cut the file and directory
* @ Param string $ to the path of the target file
* @ Param string $ from source file path
*/
Function moveDir ($ from, $ ){
If (! Is_dir ($ from) {// judge whether the $ from source file directory exists
Return false;
 }

$ From = rtrim (str_replace ('//', '/', $ from), '/'); // For linux compatibility, we can convert all the/symbols to/Because the two symbols under windows are acceptable.
$ Files = scandir ($ from); // list the files and folders in the source file directory, and store $ files as an array.

/*

$ Files output result:

Array ([0] =>. [1] => .. [2] => a [3] => B [4] => c [5] => dir [6] => dir. php [7] => dir2 [8] => dir2.php [9] => function_file.php [10] => homework. php)
The scandir function outputs two redundant values: [0] =>. [1] => .. this function is useful to us. Write an if statement to kill them.
*/
Foreach ($ files as $ file) {// traverses the $ files array to conveniently copy and delete folders and files in the array.
If (in_array ($ file, array ('. ','.. ') {// array ('. ','.. ') a new one is only. and .. and find the $ file. and .. these two values
Continue;
  }
$ SubFrom = $ from. '/'. $ file; // Convert the folder or file name after traversal to a new path.
$ SubTo = $ to. '/'. $ file;

If (is_dir ($ subFrom )){
@ Mkdir ($ subTo); // judge whether $ subFrom is a directory. If it is a directory, create a directory under the Target Directory.
MoveDir ($ subFrom, $ subTo); // recursively execute the new directory.
} Else {// if it is not a directory, copy the file directly. After copying, delete the file.
Copy ($ subFrom, $ subTo );
Unlink ($ subFrom); // delete all files
  }
@ Rmdir ($ subFrom); // delete all directories
 }
Return true;
}
MoveDir ('C:/Users/Administrator/Desktop/0704 & prime;, 'E: '); // The address of the file or directory to be moved.

Method 2: rename

1. For files, rename can be moved between different drive letters.

2. For empty folders, rename can be moved between different drive letters, but the parent directory of the target folder must exist.

3. For non-empty folders, they can only be moved under the same drive letter. However, 1 and 3 should be able to deal with almost all applications.

The code is as follows: Copy code


<? Php
Rename ("D:/testdir/test", "F:/totestdir/mydir ");
?>

For a 40 m file, the copy + unlink method takes 7.6249899864197 seconds, while the rename method only requires 0.024738788604736, 300 times faster.

Example

The code is as follows: Copy code

<? Php
// Define a variable and save the file name
$ File = "html/cache.txt ";
$ Rename = "html/renameCache.txt ";
// Rename an object using the rename () function
If (rename ($ file, $ rename) = TRUE ){
Echo "file renamed! ";
} Else {
Echo "an error occurred while renaming the file! ";
}
// Use the rename () function to move the file and rename it
Rename ("html/renameCache.txt", "html/a/2.txt ");
// Rename the Directory using the rename () function
Rename ("html", "cache ");
// Use the rename () function to move the directory to the target Directory
Rename ("B", "cache/B ");
?>

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.