1

I have the following function which is supposed to help copy folders and files inside folders and files. I would like to copy the following source :

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

To the following destination :

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

So the folder changing is Activity No 1 -> Asqwerty , I tried the following function but it didn't work :

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

But it only copies to the following level :

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


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

How can I sought this out ?

H Dindi
  • 1,484
  • 6
  • 39
  • 68
  • Possible duplicate of [Copy entire contents of a directory to another using php](http://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php) – MonkeyZeus Aug 10 '16 at 21:03
  • @MonkeyZeus I tried it ...it worked for the parent directory but not for the secondary directory. – H Dindi Aug 11 '16 at 05:22

2 Answers2

0

To create folders and files resources when a user logs into the system usually use

contant.php

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

controller

$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());
            }
        }
    }
elddenmedio
  • 1,030
  • 1
  • 8
  • 15
0

I have not tested this but I think this should work:

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);
    }
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77