I want to move all files and folders inside a folder to another folder. I found a code to copy all files inside a folder to another folder. move all files in a folder to another
我想将文件夹中的所有文件和文件夹移动到另一个文件夹。我找到了一个代码将文件夹中的所有文件复制到另一个文件夹。将文件夹中的所有文件移动到另一个
// 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);
}
How do I change this to move all folders and files inside this folder to another folder.
如何更改此项以将此文件夹中的所有文件夹和文件移动到另一个文件夹。
6 个解决方案
#1
22
This is what i use
这就是我用的
// 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 );
}
Usage
用法
rcopy($source , $destination );
Another example without deleting destination file or folder
另一个示例没有删除目标文件或文件夹
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);
}
Please See: http://php.net/manual/en/function.copy.php for more juicy examples
请参阅:http://php.net/manual/en/function.copy.php了解更多有用的例子
Thanks :)
谢谢 :)
#2
17
Use rename
instead of copy
.
使用重命名而不是复制。
Unlike the C function with the same name, rename
can move a file from one file system to another (since PHP 4.3.3 on Unix and since PHP 5.3.1 on Windows).
与具有相同名称的C函数不同,重命名可以将文件从一个文件系统移动到另一个文件系统(因为Unix上的PHP 4.3.3和Windows上的PHP 5.3.1)。
#3
9
Think this should do the trick: http://php.net/manual/en/function.shell-exec.php
认为这应该做的伎俩:http://php.net/manual/en/function.shell-exec.php
shell_exec("mv sourcedirectory path_to_destination");
Hope this help.
希望这有帮助。
#4
9
Move_Folder_To("./path/old_folder_name", "./path/new_folder_name");
function code:
功能码:
function Move_Folder_To($source, $target){
if( !is_dir($target) ) mkdir(dirname($target),null,true);
rename( $source, $target);
}
#5
0
$src = 'user_data/company_2/T1/';
$dst = 'user_data/company_2/T2/T1/';
rcopy($src, $dst); // Call function
// 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 );
rrmdir ( $src );
}
// 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);
}
#6
0
I use it
我用它
// function used to copy full directory structure from source to target
function full_copy( $source, $target )
{
if ( is_dir( $source ) )
{
mkdir( $target, 0777 );
$d = dir( $source );
while ( FALSE !== ( $entry = $d->read() ) )
{
if ( $entry == '.' || $entry == '..' )
{
continue;
}
$Entry = $source . '/' . $entry;
if ( is_dir( $Entry ) )
{
full_copy( $Entry, $target . '/' . $entry );
continue;
}
copy( $Entry, $target . '/' . $entry );
}
$d->close();
} else {
copy( $source, $target );
}
}
#1
22
This is what i use
这就是我用的
// 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 );
}
Usage
用法
rcopy($source , $destination );
Another example without deleting destination file or folder
另一个示例没有删除目标文件或文件夹
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);
}
Please See: http://php.net/manual/en/function.copy.php for more juicy examples
请参阅:http://php.net/manual/en/function.copy.php了解更多有用的例子
Thanks :)
谢谢 :)
#2
17
Use rename
instead of copy
.
使用重命名而不是复制。
Unlike the C function with the same name, rename
can move a file from one file system to another (since PHP 4.3.3 on Unix and since PHP 5.3.1 on Windows).
与具有相同名称的C函数不同,重命名可以将文件从一个文件系统移动到另一个文件系统(因为Unix上的PHP 4.3.3和Windows上的PHP 5.3.1)。
#3
9
Think this should do the trick: http://php.net/manual/en/function.shell-exec.php
认为这应该做的伎俩:http://php.net/manual/en/function.shell-exec.php
shell_exec("mv sourcedirectory path_to_destination");
Hope this help.
希望这有帮助。
#4
9
Move_Folder_To("./path/old_folder_name", "./path/new_folder_name");
function code:
功能码:
function Move_Folder_To($source, $target){
if( !is_dir($target) ) mkdir(dirname($target),null,true);
rename( $source, $target);
}
#5
0
$src = 'user_data/company_2/T1/';
$dst = 'user_data/company_2/T2/T1/';
rcopy($src, $dst); // Call function
// 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 );
rrmdir ( $src );
}
// 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);
}
#6
0
I use it
我用它
// function used to copy full directory structure from source to target
function full_copy( $source, $target )
{
if ( is_dir( $source ) )
{
mkdir( $target, 0777 );
$d = dir( $source );
while ( FALSE !== ( $entry = $d->read() ) )
{
if ( $entry == '.' || $entry == '..' )
{
continue;
}
$Entry = $source . '/' . $entry;
if ( is_dir( $Entry ) )
{
full_copy( $Entry, $target . '/' . $entry );
continue;
}
copy( $Entry, $target . '/' . $entry );
}
$d->close();
} else {
copy( $source, $target );
}
}