我想将文件夹中的所有文件和文件夹移动到另一个文件夹.我找到了将文件夹中的所有文件复制到另一个文件夹的代码. 将文件夹中的所有文件移动到另一个
// Get array of all source files
$files = scandir("source");
// Identify directories
$source = "source/";
$destination = "destination/";
// Cycle through all source files
foreach ($files as $file) {
if (in_array($file, array(".",".."))) continue;
// If we copied this successfully, mark it for deletion
if (copy($source.$file, $destination.$file)) {
$delete[] = $source.$file;
}
}
// Delete all successfully-copied files
foreach ($delete as $file) {
unlink($file);
}
Run Code Online (Sandbox Code Playgroud)
如何更改此项以将此文件夹中的所有文件夹和文件移动到另一个文件夹.
Bab*_*aba 26
这是我用的
// Function to remove folders and files
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
// Function to Copy folders and files
function rcopy($src, $dst) {
if (file_exists ( $dst ))
rrmdir ( $dst );
if (is_dir ( $src )) {
mkdir ( $dst );
$files = scandir ( $src );
foreach ( $files as $file )
if ($file != "." && $file != "..")
rcopy ( "$src/$file", "$dst/$file" );
} else if (file_exists ( $src ))
copy ( $src, $dst );
}
Run Code Online (Sandbox Code Playgroud)
用法
rcopy($source , $destination );
Run Code Online (Sandbox Code Playgroud)
另一个示例没有删除目标文件或文件夹
function recurse_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
Run Code Online (Sandbox Code Playgroud)
请参阅:http://php.net/manual/en/function.copy.php了解更多有用的例子
谢谢 :)
T.T*_*dua 10
你需要自定义功能:
Move_Folder_To("./path/old_folder_name", "./path/new_folder_name");
Run Code Online (Sandbox Code Playgroud)
功能代码:
function Move_Folder_To($source, $target){
if( !is_dir($target) ) mkdir(dirname($target),null,true);
rename( $source, $target);
}
Run Code Online (Sandbox Code Playgroud)
认为这应该做的伎俩:http: //php.net/manual/en/function.shell-exec.php
shell_exec("mv sourcedirectory path_to_destination");
Run Code Online (Sandbox Code Playgroud)
希望这有帮助.