PHP - ftp_put +复制整个文件夹结构 [英] PHP - ftp_put + Copy Entire Folder Structure

查看:136
本文介绍了PHP - ftp_put +复制整个文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个FTP脚本,将本地文件夹结构复制到FTP点。基本上更新一个网站。



我一直在测试下面的代码(已经改变了用户/密码/域名),但连接并没有失败,似乎正在工作。

  $ server ='ftp.domainname.co'; 
$ ftp_user_name ='user';
$ ftp_user_pass ='pass';
$ dest ='。';
$ source ='。';
$ mode ='FTP_ASCII';


$ connection = ftp_connect($ server);

$ login = ftp_login($ connection,$ ftp_user_name,$ ftp_user_pass);

if(!$ connection ||!$ login){die('Connection attempt failed!'); }

$ upload = ftp_put($ connection,$ dest,$ source,$ mode);

if(!$ upload){echo'FTP upload failed!'; }

ftp_close($ connection);

我相信突破点是ftp_put线。
我的问题是:


  1. ftp_put可以上传整个目录结构和文件等,或者只是上传一个文件一次?是否有一个不同的命令我应该使用?


  2. 我想我有这些变量的错误:

      $ dest ='。'; 
    $ source ='。';
    $ mode ='FTP_ASCII';


我相信模式是正确的。



$ dest - 这只是ftp服务器的根目录ftp.domainname.co - 我应该把ftp服务器名称还是放在这里。



$ source - 这是当前的本地路径 - 我也尝试了完整的C:\ etc路径。



我得到这个错误:
警告:ftp_put()期望参数4很长



任何帮助都很好。



解决方案

它不工作,因为它期望的是文件而不是目录。
ftp_put的PHP手册有一些递归文件上传的代码示例

以下是其中之一(请注意,它需要一个完整路径):

 函数ftp_putAll($ conn_id,$ src_dir,$ dst_dir){
$ d = dir($ src_dir);
while($ file = $ d-> read()){//为目录
中的每个文件执行此操作if($ file!=。&& $ file!= ...){//防止无限循环
if(is_dir($ src_dir。/。$ file)){//如果它是目录
if(! @ftp_chdir($ conn_id,$ dst_dir。/。$ file)){
ftp_mkdir($ conn_id,$ dst_dir。/。$ file); //创建不存在的目录
}
ftp_putAll($ conn_id,$ src_dir。/。$ file,$ dst_dir。/。$ file); //递归部分
} else {
$ upload = ftp_put($ conn_id,$ dst_dir。/。$ file,$ src_dir。/。$ file,FTP_BINARY); //把文件
}
}
}
$ d-> close();
}


I'm trying to create an FTP script that will copy a local folder structure upto an FTP point. Basically to update a website.

I have been testing the following code (have changed the user/pass/domain) but the connection doesn't fail and appears to be working.

   $server = 'ftp.domainname.co';
   $ftp_user_name = 'user';
   $ftp_user_pass = 'pass';
   $dest = '.';
   $source = '.';
   $mode = 'FTP_ASCII';


   $connection = ftp_connect($server);

   $login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

   if (!$connection || !$login) { die('Connection attempt failed!'); }

   $upload = ftp_put($connection, $dest, $source, $mode);

   if (!$upload) { echo 'FTP upload failed!'; }

   ftp_close($connection); 

I'm confident the point that is breaking is the ftp_put line. My questions are:

  1. Can ftp_put upload an entire directory structure with files etc or is this just to upload one file at a time? Is there a different command I should be using?

  2. I think I have something wrong with these variables:

        $dest = '.';
        $source = '.';
        $mode = 'FTP_ASCII';
    

I believe mode is correct.

$dest - this is just the root of the ftp server being ftp.domainname.co - should I put the ftp server name or what goes here.

$source - this is a the current local path - I've also tried the full C:\etc path.

I get this error: Warning: ftp_put() expects parameter 4 to be long

any help would be great.

thx

解决方案

It is not working because it is expecting a file, not a directory. PHP manual for ftp_put has some code examples for recursive file uploads posted by commenters.

Here is one of them (note that it requires a full path):

function ftp_putAll($conn_id, $src_dir, $dst_dir) {
    $d = dir($src_dir);
    while($file = $d->read()) { // do this for each file in the directory
        if ($file != "." && $file != "..") { // to prevent an infinite loop
            if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
                if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
                    ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
                }
                ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
            } else {
                $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
            }
        }
    }
    $d->close();
}

这篇关于PHP - ftp_put +复制整个文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆