Codeigniter递归复制文件夹和文件

时间:2016-08-10 20:22:12

标签: php codeigniter

我有以下功能,可以帮助复制文件夹和文件中的文件夹和文件。我想复制以下来源:

Documents/Projects/Sensitization Workshop Meeting/Milestones/Activity No 1/Tasks/Task One/New File.txt

到以下目的地:

Documents/Projects/Sensitization Workshop Meeting/Milestones/Asqwerty/Tasks/Task One/ New File.txt

因此文件夹更改为活动编号1 - >阿斯克蒂, 我尝试了以下功能,但它不起作用:

 // copy recursive
    function recursive_copy($source, $dest) {
        if (is_dir($source)) {
            $dir_handle = opendir($source);

            while ($file = readdir($dir_handle)) {
                if ($file != "." && $file != "..") {
                    if (is_dir($source . "/" . $file)) {
                        if (!is_dir($dest . "/" . $file)) {

                            mkdir($dest . "/" . $file, 0777, TRUE);
                        }

                        $this->recurse_copy($source . '/' . $file, $dest . '/' . $file);
                    } else {

                        copy($source . "/" . $file, $dest . "/" . $file);
                    }
                }
            }
            closedir($dir_handle);
        } else {
            copy($source, $dest);
        }
    }

但它只复制到以下级别:

Source : Documents/Projects/Sensitization Workshop Meeting/Milestones/Activity No 1/Tasks


Destination : Documents/Projects/Sensitization Workshop Meeting/Milestones/Asqwerty/Tasks

我怎样才能解决这个问题?

2 个答案:

答案 0 :(得分:0)

要在用户登录系统时创建文件夹和文件资源,通常使用

contant.php

define('DIR_WRITE_MODE', 0777);
define('BASE_PRINCIPAL',    'resources/base_theme_member/web2');
define('MEMBER_RESOURCES',  '/resources/');

控制器

$source     = BASE_PRINCIPAL;
$dest           = MEMBER_PRINCIPAL . $user_id . MEMBER_RESOURCES;

private function _create_basic_themplates( $user_id, $source, $dest ){
        // recursive function to copy
        // all subdirectories and contents:
        mkdir($dest, DIR_WRITE_MODE, TRUE);

        foreach(
                $iterator = new \RecursiveIteratorIterator(
                                                            new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
                                                            \RecursiveIteratorIterator::SELF_FIRST) as $item
                ){
            if( $item->isDir()){
                mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), DIR_WRITE_MODE, TRUE);
            } 
            else{
                copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
            }
        }
    }

答案 1 :(得分:0)

我没有对此进行过测试,但我认为这应该可行:

function recursive_copy($source, $dest)
{
    if(is_dir($source)))
    {
        if(!is_dir($dest))
        {
            mkdir($dest, 0777, true);
        }

        $dir_items = array_diff(scandir($source), array('..', '.'));

        if(count($dir_items) > 0)
        {
            foreach($dir_items as $v)
            {
                $this->recursive_copy(rtrim(rtrim($source, '/'), '\\').DIRECTORY_SEPARATOR.$v, rtrim(rtrim($dest, '/'), '\\').DIRECTORY_SEPARATOR.$v);
            }
        }
    }
    elseif(is_file($source)))
    {
        copy($source, $dest);
    }
}