繁体   English   中英

通过浏览器下载PHP cURL文件

[英]PHP cURL download file by browser

我创建了从某些文件服务器下载文件的脚本,但运行不正常。

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

当从服务器键入$ url到文件(mp3)时,将下载此文件,但没有正确的文件大小。 示例:服务器上的文件大小为4.5mb,我的脚本下载了文件,但大小为6mb。 音乐正在播放,但每秒中断一次。 你知道哪里有问题吗?

我尝试在标头中设置content-lenght,但每个函数返回的内容lengh = 0

试试这个,对我来说下载大型文件很有用

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

此代码是工作的罚款和我在一起,我使用PHP readfile函数从远程主机中为每个文件读取文件readfile

如果启用了fopen_wrappers,则可以将URL用作此功能的文件名。 有关如何指定文件名的更多详细信息,请参见fopen()。 请参阅受支持的协议和包装器,以获取有关各种包装器具有的功能,其使用说明以及它们可能提供的任何预定义变量的信息的链接。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM