duanhao9176 2015-08-22 12:50
浏览 719

PHP cURL通过浏览器下载文件

Im create script to download file from some file server but not work perfectly.

        function downloadPage2($url){

        $ch = curl_init();
        $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);   
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {

            header('Expires: 0'); // no cache
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
            header('Cache-Control: private', false);
            header('Content-Type: application/force-download');
            header('Content-Disposition: attachment; filename="mp3.mp3"');
            header('Content-Transfer-Encoding: binary');
//          header('Content-Length: ' . strlen($buffer)); // provide file size
            header('Connection: close');
            echo $buffer;
              return strlen($buffer);
            });
        curl_exec ($ch);
        curl_close($ch);
    }

And when type $url to file (mp3) from server its download this file but not right filesize. Example: File on server has size 4.5mb and my script downloaded it but size is 6mb. Music is playing but interrupts per second. Do you know where is a problem?

I tried set content-lenght in header but every function return content lengh = 0

  • 写回答

2条回答 默认 最新

  • doubiantiao4994 2015-08-22 13:18
    关注

    Try this, it worked for me download large files

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $source);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // if https
    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    // or set this option
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($ch);
    
    $error = curl_error($ch);
    $file = $data = curl_exec($ch);
    curl_close($ch);
    if (file_exists($file)) {
      header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
      header('Content-Disposition: attachment; filename="sometrack.mp3"');
      header("Content-Transfer-Encoding: binary");
      header('Content-length: ' . filesize($file));
      header('X-Pad: avoid browser bug');
      header('Cache-Control: no-cache');
      readfile($file);
    } else {
      echo "no file";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题