php 将一个文件夹中的所有文件和文件夹移动到另一个文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9835492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:44:17  来源:igfitidea点击:

move all files and folders in a folder to another?

phpwindows

提问by Sara

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.

如何更改此设置以将此文件夹内的所有文件夹和文件移动到另一个文件夹。

回答by Baba

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.phpfor more juicy examples

请参阅:http: //php.net/manual/en/function.copy.php了解更多有趣的例子

Thanks :)

谢谢 :)

回答by Joni

Use renameinstead of copy.

使用rename代替copy

Unlike the C function with the same name, renamecan 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 函数不同,rename可以将文件从一个文件系统移动到另一个文件系统(自 Unix 上的 PHP 4.3.3 和 Windows 上的 PHP 5.3.1 起)。

回答by T.Todua

You need the custom function:

您需要自定义函数:

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

回答by metalfight - user868766

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.

希望这有帮助。

回答by chybeat

I think the answers are not complete for me, beacuse DIRECTORY_SEPARATORare not defined on any answer (thans to Edgar Aivarsfor remember that!), but I want to write my solution for move (rename), copy and delete directory structures (based on this post info, the credits are for yours!).

我认为答案对我来说并不完整,因为DIRECTORY_SEPARATOR没有在任何答案中定义(比Edgar Aivars记住这一点!),但我想编写我的移动(重命名)、复制和删除目录结构的解决方案(基于这个帖子信息,学分是给你的!)。

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

I hope this hepls anyone! (lile me in the future :D )

我希望这对任何人都有帮助!(在未来让我:D)

EDIT: Spell corrections... :(

编辑:拼写更正... :(

回答by Neeraj

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

回答by neeraj

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