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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)