Php mobile folder and file program code

Source: Internet
Author: User
In php, we use copy to copy files. next we will look at a simple example. he has moved directories and directory files. I hope this article will help you. Method 1: copy + unlink my idea is: move new + delete. Before moving the file, we use copy to move the file in the object... php. let's take a look at a simple example. he has moved the directory and the directory file. I hope this article will help you.

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.

 . [1] => .. [2] => a [3] => B [4] => c [5] => dir [6] => dir. php [7] => dir2 [8] => dir2.php [9] => function_file.php [10] => homework. php) you can see that the scandir function outputs two redundant values: [0] =>. [1] => .. this is useful to us. write an if statement to kill them. */Foreach ($ files as $ file) {// traverses the $ files array for copying and deleting 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 are continue;} $ subFrom = $ from. '/'. $ file; // Convert the folder or file name after traversal into a new path $ subTo = $. '/'. $ file; if (is_dir ($ subFrom) {@ mkdir ($ subTo); // checks whether $ subFrom is a directory. if it is a directory, it is under the Target Directory, create a 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/100', '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.


 

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

 


Article link:

Save this article for favorites!

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.