PHP Move copied files We use copy, below we look at a simple example, he realized the directory and directory files move, I hope this article to help you.
Method One, Copy+unlink way
My idea is: move = new + DELETE. Create a new folder in the target directory before moving it, and then copy the files and directories in the past, and then delete them at the end of the execution.
The code is as follows |
Copy Code |
/** * @parammoveDir cut files and directories * @param string $to destination file path * @param string $from source file path */ function Movedir ($from, $to) { if (!is_dir ($from)) {//Determine $from source file directory is not present return false; } $from = RTrim (Str_replace ('///', '/', $from), '/');//To be compatible with Linux, we convert/sign all to/because 2 symbols are available under Windows. $files = Scandir ($from),//lists the files and folders of the source file directory, and deposits the $files as an array. /* $files output: Array ([0] = =. [1] = =. [2] = a [3] = b [4] = = c [5] = dir [6] = dir.php [7] = = DIR2 [8] = = dir2.php [9] = Function_f ile.php [Ten] = homework.php) You can see that the Scandir function outputs 2 extra values: [0] + =. [1] = =. It is useful for us wood. Write a if kill them. */ The foreach ($files as $file) {//$files array is traversed to facilitate the copying and deletion of the folders and files inside the group. if (In_array ($file, Array ('. ', '. '))) {//Array ('. ', ' ... ') ) A new one with only. and. And look for $file inside to know if there are any. and. These 2 values Continue } $subFrom = $from. ' /'. $file;//Change the folder or file name after the Traverse to a new path $subTo = $to. ' /'. $file; if (Is_dir ($subFrom)) { @mkdir ($subTo);//Determine $subfrom is not a directory, if it is the directory under the target directory, create a new directory Movedir ($subFrom, $subTo);//execute new directory recursively. If the}else{//is not a directory, copy the file directly. Delete the file after copying it. Copy ($subFrom, $subTo); Unlink ($subFrom);//Delete all Files } @rmdir ($subFrom);//Delete all directories } return true; } Movedir (' c:/users/administrator/desktop/0704′, ' e: ');//In this pass the address of the file or directory you want to move |
Method Two, rename
1. For files, rename can be moved between different drive characters.
2. For empty folders, rename can also move between different drive characters. However, the parent directory of the destination folder must exist.
3. For non-empty folders, you can only move under the same drive. However, 1 and 3 should be able to deal with almost any application situation.
The code is as follows |
Copy Code |
Rename ("D:/testdir/test", "F:/totestdir/mydir"); ?>
|
For a 40M file, the Copy+unlink method takes 7.6249899864197 seconds, while the Rename method requires only 0.024738788604736, 300 times times faster.
Cases
The code is as follows |
Copy Code |
Define a variable, save the file name $file = "Html/cache.txt"; $rename = "Html/renamecache.txt"; Rename a file using the rename () function if (rename ($file, $rename) ==true) { echo "Rename file succeeded!"; }else{ echo "Renaming file failed!"; } Use the rename () function to move the file and rename it Rename ("Html/renamecache.txt", "html/a/2.txt"); Rename a table of contents using the rename () function Rename ("HTML", "cache"); Use the rename () function to move the directory to the target directory Rename ("B", "cache/b"); ?> |
http://www.bkjia.com/PHPjc/633122.html www.bkjia.com true http://www.bkjia.com/PHPjc/633122.html techarticle PHP Move copied files We use copy, below we look at a simple example, he realized the directory and directory files move, I hope this article to help you. Method One, Copy+unl ...