переместить все файлы и папки из одной папки в другую?
Я хочу переместить все файлы и папки внутри папки в другую папку. Я нашел код для копирования всех файлов внутри папки в другую папку. переместить все файлы из одной папки в другую
// 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);
}
Как мне изменить это, чтобы переместить все папки и файлы внутри этой папки в другую папку.
6 answers
Это то, что я использую
// 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 );
}
Использование
rcopy($source , $destination );
Другой пример без удаления целевого файла или папки
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);
}
Пожалуйста, смотрите: http://php.net/manual/en/function.copy.php для более сочных примеров
Спасибо :)
Использовать rename
вместо того, чтобы copy
.
В отличие от функции C с тем же именем, rename
может перемещать файл из одной файловой системы в другую (начиная с PHP 4.3.3 в Unix и начиная с PHP 5.3.1 в Windows).
Думаю, это должно сработать: http://php.net/manual/en/function .shell-exec.php
shell_exec("mv sourcedirectory path_to_destination");
Надеюсь, это поможет.
Move_Folder_To("./path/old_folder_name", "./path/new_folder_name");
Код функции:
function Move_Folder_To($source, $target){
if( !is_dir($target) ) mkdir(dirname($target),null,true);
rename( $source, $target);
}
$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);
}
Я использую его
// 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 );
}
}