首页 > 解决方案 > FTP 文件下载被截断,显示状态码 150

问题描述

我正在从同一服务器检索多个文件,但一个成功的代码为 226,而其他文件都被截断到 5% 到 90% 之间并返回状态代码 150。有人可以帮我理解为什么会发生这种情况吗?我能做些什么呢?

这是我的代码:

$data_file = 'file1';
// dummy site shown here
$data_file_url = 'ftp://ftp.xxxxxxxxxxxx.com/xxxxxxx/' . $data_file;
$file_handle = fopen($data_file, 'w') or die('Cannot open file: ' . $data_file);

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

curl_setopt($ch, CURLOPT_URL, $data_file_url);
curl_setopt($ch, CURLOPT_FILE, $file_handle);

$userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

// Based on searching the web, I've tried adding in the following options, but none of them helped
// curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
// curl_setopt($ch, CURLOPT_IGNORE_CONTENT_LENGTH, 1);
// curl_setopt($ch, CURLOPT_MAXFILESIZE, 10000000);
// curl_setopt($ch, CURLOPT_USERPWD, "anonymous:");

$html = curl_exec($ch);
$info = curl_getinfo($ch);
  echo print_r($info, true) . " \n";
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
fclose($file_handle);

如您所见,我在 curl_exec 之后直接回显 curl_getinfo。我尝试下载的文件之一成功。请注意,在回显输出中,response_code 为 226 - 表示“下载成功”。[size_download] = [download_content_length] = 1678549。

Array
(
    [url] => ftp://ftp.xxxxxxxxxxxx.com/xxxxxxx/file4
    [content_type] => 
    [http_code] => 226
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.929607
    [namelookup_time] => 7.3E-5
    [connect_time] => 0.081319
    [pretransfer_time] => 0.889325
    [size_upload] => 0
    [size_download] => 1678549
    [speed_download] => 572960
    [speed_upload] => 0
    [download_content_length] => 1678549
    [upload_content_length] => -1
    [starttransfer_time] => 0.912532
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => ###.###.###.###
    [certinfo] => Array
        (
        )

    [primary_port] => 50597
    [local_ip] => 192.168.142.146
    [local_port] => 44406
)

其他人的回显输出类似于以下内容。请注意,response_code 为 150 - 表示“文件状态正常;即将打开数据连接”。并且 [size_download] < [download_content_length]。

Array
(
    [url] => ftp://ftp.xxxxxxxxxxxx.com/xxxxxxx/file1
    [content_type] => 
    [http_code] => 150
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.105286
    [namelookup_time] => 4.2E-5
    [connect_time] => 0.080398
    [pretransfer_time] => 2.05937
    [size_upload] => 0
    [size_download] => 29312
    [speed_download] => 13923
    [speed_upload] => 0
    [download_content_length] => 2500140
    [upload_content_length] => -1
    [starttransfer_time] => 2.083821
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => ###.###.###.###
    [certinfo] => Array
        (
        )

    [primary_port] => 50342
    [local_ip] => 192.168.142.146
    [local_port] => 57900
)

如果连接“即将打开”,那么为什么我们会得到部分/截断的数据,而不是什么都没有?在某些文件上收到 150 而在其他文件上没有收到 150 的根本原因是什么?这些文件的大小范围从 1.6M 到 6.6M。最小的文件是下载的文件,但它似乎与大小无关,因为下一个最小的文件获取 50% 的数据,第三大的文件获取 90% 的数据。

标签: phpcurl

解决方案


推荐阅读