PHP cURL通过浏览器下载文件

我创建脚本以从某个文件服务器下载文件但不能正常工作。</ p>

< pre> function downloadPage2($ url){

$ ch = curl_init();
$ userAgent ='Mozilla / 4.0(兼容; 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 ,CURL_OPopt,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 MYH: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) ); //提供文件大小
标题('连接:关闭');
echo $ buffer;
返回strlen($ b uffer ;;
});
curl_exec($ ch);
curl_close($ ch);
}
</ code> </ pre>

输入$ url to 文件(mp3)从服务器下载此文件但不是文件大小。 示例:服务器上的文件大小为4.5mb,我的脚本下载了它,但大小为6mb。 音乐正在播放,但每秒都在中断。
你知道问题出在哪里吗?</ p>

我尝试在标题中设置content-lenght,但每个函数都返回内容lengh = 0 </ p>
</ DIV>

展开原文

原文

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

dongwuli5105
dongwuli5105 请参阅下面的更新答案,添加您需要的标题选项,这应该是一个起点。每个服务器的要求都不同,您只需要排除故障即可。
大约 5 年之前 回复
doubei2340
doubei2340 我不知道,我测试了所有选项,而且现在哪个是正确的:D..coockie是必需的
大约 5 年之前 回复
dongya3627
dongya3627 `你需要设置所有这些选项吗?
大约 5 年之前 回复

2个回答



试试这个,它适用于我下载大文件</ p>

  $ ch = curl_init  (); 
curl_setopt($ ch,CURLOPT_URL,$ source);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
//如果https
curl_setopt($ ch,CURLOPT_SSLVERSION,3);
//或设置此项 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:避免浏览器错误');
标题('Cache-Control:no-cache');
readfile($ file);
} else {
echo “没有文件”;
}
</ code> </ pre>
</ div>

展开原文

原文

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";
}

doupiai5597
doupiai5597 嗯,这是与以前相同的脚本,没有更新。
大约 5 年之前 回复
dtdsbakn210537
dtdsbakn210537 检查我更新的答案,我确实删除了文件下载。 这应该为你做的伎俩。
大约 5 年之前 回复
douxi6903
douxi6903 我添加了带有cookie的setopt及其工作。 但是......第一个脚本将所有文件字节下载到缓存中,然后将弹出窗口返回给用户进行保存。 我们不想等待很多时间作为服务器(脚本)下载文件。 脚本必须实时流式传输文件。
大约 5 年之前 回复
doufutao4428
doufutao4428 对不起,有点跳了枪,确实对你的问题提供了错误的答案,更新了我的答案。
大约 5 年之前 回复
dongtui4038
dongtui4038 但我不希望我的服务器上的磁盘上的下载文件,但使用浏览器。 Normalny就像用户点击网站上的“下载”一样,弹出文件。
大约 5 年之前 回复

This code is working fine with me, I am using php readfile function to read the file from a remote host as per documentation for readfile:

A URL can be used as a filename with this function if the fopen_wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

        header('Content-Type:'.$this->get_mime_type($url));
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=".$filename);
        echo readfile($url);


 function get_mime_type($filename) {
    $idx = explode( '.', $filename );
    $count_explode = count($idx);
    $idx = strtolower($idx[$count_explode-1]);

    $mimet = array(
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',


        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    if (isset( $mimet[$idx] )) {
        return $mimet[$idx];
    } else {
        return 'application/octet-stream';
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐