【问题标题】:The first argument to copy() function cannot be a directory?copy() 函数的第一个参数不能是目录?
【发布时间】:2011-03-24 19:01:02
【问题描述】:
    $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;

更新:根据 Pascal 的建议答案,这是我修改后的代码。这行得通。

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

【问题讨论】:

    标签: php file-io copy spl


    【解决方案1】:

    不,copy() 函数不是递归的:它不能复制文件夹及其内容。


    但是,如果您查看该手册页上的用户说明,您会发现一些递归实现的建议。

    例如,here's a recursive function proposed by gimmicklessgpt(引用他的帖子)

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



    编辑问题后编辑:

    您正在调用您的函数并传递参数:

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

    但是你的函数被定义为不带参数:

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

    您应该更正函数的定义,因此它需要参数 - 而不是在函数内部初始化那些 $src$dst,而是在第一次调用时。

    【讨论】:

    • @Scott 不客气 :-) (我已经编辑了我的答案,并添加了一些关于你的递归函数的附加说明)
    • 是的,我在更新我的问题后才意识到错误。在我修复它之前你一定已经看到了 :-)
    • 仅供参考,这不会创建目标目录结构。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 2023-02-26
      • 2015-10-06
      • 2014-04-11
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多