duanjianao0592 2017-06-23 10:56
浏览 55
已采纳

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

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 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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败