The first argument to copy() function cannot be a directory?

16,964

Solution 1

No, the copy() function is not recursive : it cannot copy folders and their contents.


But if you take a look at the users's notes on that manual page, you'll find some propositions of recursive implementations.

For example, here's a recursive function proposed by gimmicklessgpt (quoting his post) :

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



Edit after the edit of the question :

You are calling your function passing it parameters :

recurse_copy($src . '/' . $file,$dst . '/' . $file); 

But your function is defined as taking no parameter :

function recurse_copy(){
    $src = dirname(__FILE__).'/themes/';
    $dst = dirname( STYLESHEETPATH ).'/';
    ...

You should correct your function's definition, so it takes parameters -- and not initialize those $src and $dst inside the function, but on its first call.

Solution 2

yes it cannot be a directory:
Look here for your answer to copying directories :-)

Share:
16,964
Scott B
Author by

Scott B

Updated on June 04, 2022

Comments

  • Scott B
    Scott B almost 2 years
        $base = dirname(__FILE__).'/themes/';
        $target = dirname( STYLESHEETPATH ).'/';
        $directory_folders = new DirectoryIterator($base); 
        foreach ($directory_folders as $folder) 
        {
            if (!$folder->isDot())           {
    
                echo '<p>source: '.$folder->getRealPath();
                //returns: C:\xampplite\htdocs\test\wp-content\plugins\test\themes\testtheme-1
    
                echo '<br>target: '.$target;
                //returns: C:\xampplite\htdocs\test/wp-content/themes/
    
                copy($folder->getRealPath(), $target);
                //returns: Error. The first argument to copy() function cannot be a directory
             }
        }die;
    

    UPDATE: On Pascal's suggested answer, here's my amended code. This works.

    function recurse_copy(){
        $src = dirname(__FILE__).'/themes/';
        $dst = dirname( STYLESHEETPATH ).'/';
    
        $dir = opendir($src); 
        @mkdir($dst); 
        while(false !== ( $file = readdir($dir)) ) 
        { 
            if (( $file != '.' ) && ( $file != '..' )) 
            { 
                if ( is_dir($src . '/' . $file) ) { 
                    recurse_copy_recurse($src . '/' . $file,$dst . '/' . $file); 
                } 
                else { 
                    copy($src . '/' . $file,$dst . '/' . $file); 
                } 
            } 
        } 
        closedir($dir); 
    }
    
    
    function recurse_copy_recurse($src,$dst){
    
        $dir = opendir($src); 
        @mkdir($dst); 
        while(false !== ( $file = readdir($dir)) ) 
        { 
            if (( $file != '.' ) && ( $file != '..' )) 
            { 
                if ( is_dir($src . '/' . $file) ) { 
                    recurse_copy_recurse($src . '/' . $file,$dst . '/' . $file); 
                } 
                else { 
                    copy($src . '/' . $file,$dst . '/' . $file); 
                } 
            } 
        } 
        closedir($dir); 
    }