3
    $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); 
}
4

2 回答 2

10

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


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

例如,这是gimicklessgpt提出的递归函数 (引用他的帖子)

<?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而是在第一次调用时。

于 2011-03-24T19:06:42.707 回答
2

是的,它不能是目录:
在此处查找复制目录的答案:-)

于 2011-03-24T19:03:30.523 回答