: This article mainly introduces php mobile folders and files. For more information about PHP tutorials, see. 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: |
|
/** * @ 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/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.
The code is as follows: |
|
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: |
|
// 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 "); ?> |
The above introduces php mobile folders and files, including the content, hope to be helpful to friends who are interested in PHP tutorials.