简体   繁体   English

PHP FTP复制目录递归

[英]PHP FTP copy directory recursive

For my website I need to copy one folder from the main account on my VPS to a (automaticly) newly created cPanel account. 对于我的网站,我需要将一个文件夹从VPS上的主帐户复制到一个(自动)新创建的cPanel帐户。 I tried to do this with PHP, through FTP using the following code (function): 我尝试使用以下代码(功能)通过FTP通过PHP来实现此目的:

function ftp_sync ($dir) { 
    global $conn_id;

    if ($dir != ".") { 
            if (ftp_chdir($conn_id, $dir) == false) { 
                echo ("Change Dir Failed: $dir<BR>\r\n"); 
                return; 
            } 
            if (!(is_dir($dir))) 
                mkdir($dir); 
        chdir ($dir); 
    } 

        $contents = ftp_nlist($conn_id, "."); 
    foreach ($contents as $file) { 

            if ($file == '.' || $file == '..') 
            continue; 

            if (@ftp_chdir($conn_id, $file)) { 
                    ftp_chdir ($conn_id, ".."); 
                    ftp_sync ($file); 
        } 
            else 
            ftp_get($conn_id, $file, $file, FTP_BINARY); 
        }
    }

    foreach (glob("*") as $file)
    {
        if(substr_count($file, ".") > 0)
        {
            $source_file = $file;
            $destination_file = $file;
            $upload = ftp_put($conn_id, "public_html/".$destination_file, $source_file, FTP_BINARY);
            echo "<br />";
            // check upload status
            if (!$upload) { 
                echo "FTP upload has failed!";
            } else {
                echo "Uploaded $source_file to $ftp_server as $destination_file";
            }
        }else{
            ftp_sync($dir);
        }
    } 


    ftp_chdir ($conn_id, "..");
        chdir ("..");  

} 

However it doesn't seem to work (no new directories are created and uploaded to)... Does anyone know why this isn't working and how I can make it work? 但是,它似乎不起作用(没有创建新目录,也没有将新目录上传到其中)...有谁知道为什么它不起作用以及如何使它起作用?

Thanks in advance! 提前致谢!

Best Regards, Skyfe. 最好的问候,Skyfe。

EDIT: I forgot to mention that I run the script as a cronjob script, also making sure it has all rights as it's executed from the main server. 编辑:我忘了提及我将脚本作为cronjob脚本运行,并且还要确保它具有从主服务器执行的所有权限。

First of all, make sure your directory on the target server is writable. 首先,请确保目标服务器上的目录可写。 Temporary chmodding it to 0777 should help. 暂时将其压缩到0777应该会有所帮助。 The rest of your script seems to be okay. 脚本的其余部分似乎还可以。 You could try setting the error logging to all errors (just add error_reporting(E_ALL); at the start of your script). 您可以尝试将错误日志记录设置为所有错误(只需在脚本开头添加error_reporting(E_ALL); )。 PHP should then output every error, warning or notice, which might provide you more info. 然后,PHP应该输出所有错误,警告或通知,它们可能会为您提供更多信息。

Didn't get the function to work so I recreated it my own way and got it working! 无法使用该功能,因此我以自己的方式重新创建了该功能并使其正常工作!

...
ftp_mkdir($conn_id, "public_html/".$dir);
ftp_upload($dir);

// close the FTP stream 
ftp_close($conn_id); 

function ftp_upload($dir) {
    global $conn_id;
    if($handle = opendir($dir))
    {
    while(false !== ($file = readdir($handle)))
    {
        if($file != "." && $file != ".." && $file != "...") {
        if(substr_count($file, ".") > 0)
        {
            $full_dir = "public_html/".$dir;
            $source_file = $file;
            $destination_file = $file;
            $upload = ftp_put($conn_id, $full_dir."/".$destination_file, $dir."/".$source_file, FTP_BINARY);
            echo "<br />";
            // check upload status
            if (!$upload) { 
                echo "FTP upload has failed!"; 
            } else {
                echo "Uploaded ".$source_file." to ".$ftp_server." as ".$destination_file; 
            }
        }else{
            ftp_mkdir($conn_id, "public_html/".$dir."/".$file);
            ftp_upload($dir."/".$file);
        }
        }
    }   
    }

}

Now the only thing left is making sure it works on big directory structures too (without a huge loadtime)! 现在剩下的唯一事情就是确保它也可以在大型目录结构上运行(无需花费大量的加载时间)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM