0

Sorry for new post! I'm not yet trusted to comment on others posts.

I'm having trouble copying folders and this is where I started: Copy entire contents of a directory

Function

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

My input

$src = "http://$_SERVER[HTTP_HOST]/_template/function/";
$dst = "http://$_SERVER[HTTP_HOST]/city/department/function/";
recurse_copy($src, $dst);

I've also tried this

$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/"; // And so on...

The function is executed but nothing is being copied.

Any ideas on what might be wrong?

SOLVED

Working solution

Along with

$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/";
$dst = "$_SERVER[DOCUMENT_ROOT]/city/department/function/";
recurse_copy($src, $dst);
Community
  • 1
  • 1
Andreas
  • 37
  • 1
  • 11

2 Answers2

1

It's not tested but I think the issue might be that the target directory is not necessarily being created before attempting to copy files to it. The piece of code that creates the target directory would require a folder path rather than a full filepath - hence using dirname( $dst )

if( !defined('DS') ) define( 'DS', DIRECTORY_SEPARATOR );

function recurse_copy( $src, $dst ) { 

    $dir = opendir( $src ); 
    @mkdir( dirname( $dst ) );

    while( false !== ( $file = readdir( $dir ) ) ) { 
        if( $file != '.' && $file != '..' ) { 
            if( is_dir( $src . DS . $file ) ) { 
                recurse_copy( $src . DS . $file, $dst . DS . $file ); 
            } else { 
                copy( $src . DS . $file, $dst . DS . $file ); 
            } 
        } 
    } 
    closedir( $dir ); 
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • This did the trick along with `$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/";` Thanks a bunch! – Andreas Feb 10 '16 at 09:42
0

Use local paths

$src= "_template/function/";
$dst= "city/department/function/";
recurse_copy($src, $dst);

copy works locally on your server. You're trying to copy using HTTP scheme, it's not working that way.

ksimka
  • 1,394
  • 9
  • 21
  • My document is in /admin folder so I've added "../" infront of your definitions but now the page gets stuck in loading. Same if I remove my addition (if the path always is from root?). – Andreas Feb 10 '16 at 09:38