переместить все файлы и папки из одной папки в другую?


Я хочу переместить все файлы и папки внутри папки в другую папку. Я нашел код для копирования всех файлов внутри папки в другую папку. переместить все файлы из одной папки в другую

// 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);
}

Как мне изменить это, чтобы переместить все папки и файлы внутри этой папки в другую папку.

Author: Community, 2012-03-23

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 для более сочных примеров

Спасибо :)

 22
Author: Baba, 2012-03-23 07:48:12

Использовать rename вместо того, чтобы copy.

В отличие от функции C с тем же именем, rename может перемещать файл из одной файловой системы в другую (начиная с PHP 4.3.3 в Unix и начиная с PHP 5.3.1 в Windows).

 17
Author: Joni, 2013-03-01 09:48:12

Думаю, это должно сработать: http://php.net/manual/en/function .shell-exec.php

shell_exec("mv sourcedirectory path_to_destination");

Надеюсь, это поможет.

 9
Author: metalfight - user868766, 2013-03-09 19:27:01
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);
}
 9
Author: T.Todua, 2018-05-23 15:08:07
$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);
}
 0
Author: Neeraj, 2015-06-22 05:53:32

Я использую его

// 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 );
    }
}
 0
Author: neeraj, 2017-12-16 04:32:32