php move folders and files

WBOY
Release: 2016-08-08 09:23:06
Original
3104 people have browsed it
Method one, copy+unlink methodMy idea is: move = new + delete. Before moving, create a new folder in the target directory, then copy the files and directories there, and finally delete them.
The code is as follows Copy the code

/**
* @parammoveDir Cut files and directories
* @param string $to target file path
* @param string $from source file path
* /
function moveDir($from,$to){
if(!is_dir($from)){//Determine whether the $from source file directory exists
return false;
}

$from = rtrim(str_replace(' //','/',$from), '/');//In order to be compatible on Linux, we convert all / symbols into / because both symbols under windows are acceptable.
$files = scandir($ from);//List the files and folders in the source file directory and store them in $files in the form of an array. The output of

/*

$files:

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 )
You can see that the scandir function will output 2 redundant values: [0] => . [1] => .. It is not useful to us here. Write an if to kill them.
*/
foreach($files as $file){//Traverse the $files array to facilitate copying and deleting folders and files in the array.
if(in_array($file, array('.', '..'))){// array('.','..') creates a new array with only . and .., and searches $file to see if there are 2 values ​​​​of . and ..
continue ;
}
$subFrom = $from.'/'.$file;//Convert the traversed folder or file name into a new path
$subTo = $to.'/'.$file;

if(is_dir($subFrom)){
@mkdir($subTo);//Determine whether $subFrom is a directory. If it is a directory, create a new directory under the target directory
moveDir($subFrom, $subTo); //Recursively execute the new directory.
}else{//If it is not a directory, copy the file directly. Delete the file after copying.
copy($subFrom, $subTo);
unlink($subFrom);//Delete all files
}
@rmdir($ subFrom);//Delete all directories
}
return true;
}
moveDir('C:/Users/Administrator/Desktop/0704′,'e:');//Pass the file you want to move here or Directory address

Method 2, rename1. For files, rename can move between different drive letters.2. For empty folders, rename can also move 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 handle almost all application situations.
The code is as follows Copy the code


rename("D:/testdir/test","F:/totestdir/mydir");
?>

For a 40M file, the copy+unlink method takes 7.6249899864197 seconds, while the rename method only takes 0.024738788604736, which is 300 times faster. //Define a variable to save the file name$file = "html/cache.txt";$rename = "html/renameCache.txt";//Use the rename() function to rename a fileecho "Rename file successfully!";echo "Rename file failed!";}//Use the rename() function to move the file and Rename
if(rename ($file,$rename)==TRUE){}else{
rename("html/renameCache.txt","html/a/2.txt");

//Use the rename() function to rename the directory
rename("html","cache");
//Use The rename() function moves the directory to the target directory
rename("b","cache/b");
?>





The above introduces the PHP mobile folders and files, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template