duanjianao0592
2017-06-23 10:56 阅读 50
已采纳

无法将远程文件下载到服务器

The remote file is an attachment, and i can get the headers but i cant get the file or the content of it.

 <?php
    error_reporting(E_ALL);

    //not working, with rb too
    //file_put_contents("natives.html", stream_get_contents(fopen("http://www.dev-c.com/nativedb/reference.html", 'r')));

    //same with rb too
    //file_put_contents("natives.html", fopen("http://www.dev-c.com/nativedb/reference.html", 'r'));

    //same
    //echo file_get_contents("http://www.dev-c.com/nativedb/reference.html");

    //same
    /*$url = 'http://www.dev-c.com/nativedb/reference.html';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, false);
    curl_setopt($curl, CURLOPT_HEADER, true);

    $data = curl_exec($curl);

    curl_close($curl);

    echo $data;*/

    //same
    $url = 'http://www.dev-c.com/nativedb/reference.html';
    $file = basename($url);

    $fp = fopen($file, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>

So if i use any of those codes, the output is nothing, and the created file on my webserver is empty. The file: http://www.dev-c.com/nativedb/reference.html

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongqian5639 dongqian5639 2017-06-23 12:05

    Thankyou for the help, but the problem was with the encoding. I had to send the header "Accept-Encoding: gzip" and then gzdecode the output.

    <?php
    error_reporting(E_ALL);
    
    $url = 'http://www.dev-c.com/nativedb/reference.html';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Host: www.dev-c.com",
    "Accept-Encoding: gzip"
    ));
    
    $data = curl_exec($curl);
    
    curl_close($curl);
    
    echo gzdecode($data);
    ?>
    
    点赞 评论 复制链接分享
  • doupai1876 doupai1876 2017-06-23 11:45

    I pulled the headers for the web URL that you provided with your example. This is what I got:

    HTTP/1.1 200 OK Server: nginx/1.12.0 Date: Fri, 23 Jun 2017 11:34:58 GMT Content-Type: application/octet-stream **Content-Length: 0** Connection: keep-alive Content-Description: reference.html Content-Disposition: attachment; filename=reference.html Content-Transfer-Encoding: binary Expires: Thu Jun 22 11:34:58 2017 GMT Cache-Control: must-revalidate Pragma: public Set-Cookie: PHPSESSID=f9d4eb515eeabfb461a11d056fdc7b78; path=/ Cache-Control: max-age=3600, private, proxy-revalidate

    As you can see, Content-Length: 0 is zero. That might be one of the reasons why you are getting an empty body.

    If you have access to the NGINX server that hosts reference.html, you could try to dump the contents of /var/log/nginx/error.log when you try to download reference.html.

    If Content-Length is zero, there is no file size to download. It points out to be an issue with the server setup.

    点赞 评论 复制链接分享

相关推荐