【问题标题】:Error downloading file via cURL通过 cURL 下载文件时出错
【发布时间】:2013-02-26 10:45:00
【问题描述】:

我想通过 cURL 从 ftp 服务器下载文件。 这是我的功能:

function getFile($remotefolder, $remotefile, $localfile) {
    $url  = "ftp://xxxxx.de/" . $remotefolder . "/" . $remotefile;
    $fp   = fopen($localfile, "w+");

    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($handle, CURLOPT_UPLOAD, 0);
    curl_setopt($handle, CURLOPT_FILE, $fp);
    curl_setopt($handle, CURLOPT_USERPWD, "<Username>:<Password>");
    $result = curl_exec($handle);

    $info = curl_getinfo($handle);

    curl_close($handle);
    fclose($fp);

    print_r($info);
}

如果我在我的电脑上执行,一切正常。但是当我在服务器上运行代码时,它给了我以下输出并且文件没有下载:

output server
=============
Array
(
    [url] => ftp://xxxx/xxxx/xxx.log.gz
    [content_type] =>
    [http_code] => 257
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.357453
    [namelookup_time] => 0.002011
    [connect_time] => 0.011153
    [pretransfer_time] => 1.357439
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0
    [redirect_time] => 0
)

如果您想了解更多信息,请告诉我。

提前致谢,

丹尼斯

【问题讨论】:

  • 是的,我已经阅读了这篇文章并想出了发布的代码 n-p。我的问题是这可以在我的测试机上完美运行,但不能在我的服务器上运行。而且我不知道为什么http代码是257(在我的测试机器上代码是226,这是正确的)。
  • 如果是共享主机,他们的设置可能会有一些限制。服务器运行的是什么版本?

标签: php curl ftp download


【解决方案1】:

查看您的 http_code 响应,257 是“PATHNAME”创建的代码,这可能意味着该文件不存在,或者您的路径可能不正确。

使用您知道存在的不同 URL 对其进行测试。

【讨论】:

  • 感谢您的回答。我检查了两个系统上的 URL,它们是正确的。
【解决方案2】:

尝试切换参数的顺序。这是我在我的一个类中使用的,它依赖于位于 FTP 服务器上的文件:

$fh = fopen('downloads/' . self::ZIP, 'w');
$ch = curl_init(self::FTP . self::PATH . '/' . self::ZIP);

curl_setopt($ch, CURLOPT_USERPWD, self::USERNAME . ':' . self::PASSWORD);
curl_setopt($ch, CURLOPT_FILE, $fh);

curl_exec($ch);
curl_close($ch);
fclose($fh);

【讨论】:

  • 感谢您的回答。我已经尝试过这种方式,但结果是一样的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
相关资源
最近更新 更多