Подтвердить что ты не робот

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

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

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

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

4b9b3361

Ответ 1

Это то, что я использую

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

Спасибо :)

Ответ 2

Используйте rename вместо copy.

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

Ответ 3

Вам нужна пользовательская функция:

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

Ответ 5

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

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

Ответ 6

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

Ответ 7

Я думаю, что ответы для меня не полны, потому что DIRECTORY_SEPARATOR не определены ни в одном ответе (спасибо Эдгару Айварсу за то, что помните это!), Но я хочу написать свое решение для перемещения (переименования), копирования и удаления структур каталогов (на основе этот пост информация, кредиты для вас!).

defined('DS') ? NULL : define('DS',DIRECTORY_SEPARATOR);

function full_move($src, $dst){
    full_copy($src, $dst);
    full_remove($src);
}

function full_copy($src, $dst) {
    if (is_dir($src)) {
        @mkdir( $dst, 0777 ,TRUE);
        $files = scandir($src);
        foreach($files as $file){
            if ($file != "." && $file != ".."){
                full_copy("$src".DS."$file", "$dst".DS."$file");
            }
        }
    } else if (file_exists($src)){
        copy($src, $dst);
    }
}

function full_remove($dir) {
    if (is_dir($dir)) {
        $files = scandir($dir);
        foreach ($files as $file){
            if ($file != "." && $file != ".."){
                full_remove("$dir".DS."$file");
            }
        }
        rmdir($dir);
    }else if (file_exists($dir)){
        unlink($dir);
    }
}

Я надеюсь, что это кто-нибудь hepls! (обмани меня в будущем: D)

РЕДАКТИРОВАТЬ: Исправление заклинаний... :(