【问题标题】:ftp zip files from one server to anotherftp zip 文件从一台服务器到另一台服务器
【发布时间】:2018-04-05 12:26:33
【问题描述】:

我正在尝试通过 ftp 将 zip 文件从一台服务器传输到我的服务器,以便我可以使用文件中的数据来更新我的数据库表。这是我的 ftp.php 文件:

    <?php
header('Content-type: text/html; charset=utf-8');
$date = "2013-05-21-11-19-40";
$ftp_server="ftp.server.com";
$ftp_user_name="BookCellar";
$ftp_user_pass="*****";
$file = "/reports/other/compreport_abebooks_" .$date. ".zip";//tobe uploaded
$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip"; 

?>

我的 ftpUpload.php 文件是:

    <?php
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file));
header('Content-Type: application/zip');
require_once('ftp.php');

// set up basic connection
$conn_id = ftp_ssl_connect($ftp_server);//ftp_connect

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;

} else {
echo "Connected for user $ftp_user_name" ;
}

ftp_chdir($conn_id, '/home/bookcell/bookcellaronline.com/html/testbcos/accounting/');

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
echo $php_errormsg;
// close the connection
ftp_close($conn_id);
?>

当我运行这些时,我会收到错误消息:

    [<a href='function.ftp-put'>function.ftp-put</a>]: failed to open stream: No such file or directory in /chroot/home/bookcell/bookcellaronline.com/html/testbcos/accounting/ftpUpload.php on line 25

第 25 行是:

    if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {

我研究了一堆关于 SO 的其他帖子,但没有找到解决方案。我的连接有效,但我无法传输文件。 我如何(如果可能)将其传输到我的服务器?

编辑:我是否错过了我不仅需要连接到他们的服务器($file)和我的服务器($remote_file)的事实????

【问题讨论】:

  • 您是否检查过$file 和$remote_file 均有效且路径正确?您的 PHP 用户是否具有读取本地文件的正确权限?
  • 我可以验证服务器本身的路径。 $file 在一台服务器上,而 $remote_file 在我的服务器上。两者都是正确的。

标签: php ftp


【解决方案1】:

你不能放置目标文件的路径

$remote_file = "/chroot/home/bookcellaronline.com/html/testbcos/accounting/compreport_abebooks_" .$date. ".zip";

例如- 这不起作用

ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);

你必须放

<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY ); 

一个FTP服务器隐藏了他的绝对路径/home/bookcell/bookcellaronline.com/html/

所有文件夹必须相对于root

ftp_chdir($conn_id, '/testbcos/accounting/');

测试 ftp_chdir 的结果!您在正确的目录中吗?

echo ftp_pwd($conn_id);

尝试通过浏览器连接到 FTP 服务器。

ftp://BookCellar@ftp.server.com

你得到的是/root。您在浏览器中看到的文件夹和文件位于root 目录下。

更新:开箱即用的示例。

$password = 行必须替换为 Download pass

ftp.php

<?php
    $password = "????";
    $resource = ftp_connect('ftp.strato.com');
    $login = ftp_login($resource, 'ftp_mx_all@moskito-x.de', $password);
    $list = ftp_rawlist($resource, '/');
    print_r($list);
?>

你会得到 print_r

Array ( [0] =&gt; drwxr-xr-x 2 ftp ftp 4096 May 23 20:15 aFolder [1] =&gt; -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt )

我们可以看到有一个文件夹aFolder和一个文件tutorial.txt。
我们很感兴趣aFolder 文件夹中有哪些文件?
所以将 $list = 替换为

$list = ftp_rawlist($resource, '/aFolder');

然后再次运行 php 脚本。输出:

Array ( [0] =&gt; drwxr-xr-x 3 ftp ftp 4096 May 23 19:24 .. [1] =&gt; -rw-r--r-- 1 ftp ftp 167 May 23 20:25 tutorial.txt [2] =&gt; -rw-r--r-- 1 ftp ftp 271 May 23 21:16 tutorial.zip )

现在我们要下载aFolder/tutorial.txt。
在print_r($list);下方添加以下内容。

     echo "<br />\n";
     $local_file = "tmp.txt" ;
     $file = ftp_get($resource, $local_file, '/aFolder/tutorial.txt',FTP_ASCII);
      if ($file) {
         echo "$local_file has been successfully written\n";
     } else {
         echo "An error has occurred\n";
     }

输出:

php脚本所在的文件夹发生了变化。

现在有一个新文件tmp.txt!

如果你带上这个小脚本来运行。我们可以走得更远。

来自我们的聊天:
您的服务器不允许对 url 进行 ftp 调用。

看allow_url_fopen = ON

echo ini_get('allow_url_fopen');

if (!ini_get('allow_url_fopen')) {
    ini_set('allow_url_fopen', 1);
}

echo ini_get('allow_url_fopen'); 

然后再试一次。

【讨论】:

  • @Jim:你在浏览器中得到了什么:ftp://BookCellar@ftp.server.com
  • 按原样放入时出现连接超时错误。我需要用登录信息替换 ftp.server.com 吗?
  • @Jim : 当然你需要 "ftp.server.com",替换成你的 FTP 服务器地址。
  • 我明白了:控制通道需要 550 SSL/TLS
  • 第一个连接超时,第二个找不到服务器。
【解决方案2】:
set_time_limit(0);
exec("wget --continue http://example.com/site.zip");
exit;

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 它解决问题的额外上下文将改善答案的长期性价值。
【解决方案3】:

在第一个摘录中将远程磁贴更改为

`$remote_file = "compreport_abebooks_" .$date. ".zip";`

在将您更改为目录之前。

另请注意,您在 ftp_chdir 调用中引用的目录与顶部引用的 $remote_file 中的目录不同。

 /home/bookcell/bookcellaronline.com/html/testbcos/accounting/

对比

 /chroot/home/bookcellaronline.com/html/testbcos/accounting/

【讨论】:

  • 我已经进行了建议的更改,并验证了服务器错误日志中的路径,但是我仍然收到错误消息。我需要像你一样的反勾号还是只是为了显示差异?
  • @Jim - $remote_file 的路径以 /chroot/ 开头有关系吗?您是否尝试过使用命令行 FTP 运行相同的命令集以查看是否会产生任何错误?
  • @andrewsi 没有注意到我在更新的答案中错过了它
  • 我在没有 /chroot/ 和它的情况下都试过了。当我运行 $file = realpath(dirname(FILE)).DIRECTORY_SEPARATOR.'compreport_abebooks_2013-05-21-11-19-40.zip';回声$文件;它显示 /chroot/。我可以使用 filezilla 毫无问题地下载 teh 文件,但我想自动执行此操作
猜你喜欢
  • 2018-07-08
  • 1970-01-01
  • 2016-10-18
  • 2019-01-24
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多