7

我正在尝试获取远程文件并同时强制将其下载给用户。无法粘贴代码,代码太长。但是 curl 功能可以工作,但问题是它不会输出任何内容,直到它首先获取远程文件然后强制将其下载给用户。

我用它来告诉 curl 返回一个回调

curl_setopt($ch, CURLOPT_READFUNCTION, 'readCallback');

现在在我的 readCallback 函数中我这样做:

function readCallback($curl, $stream, $maxRead){
    $read = fgets($stream, $maxRead);
    echo $read;
    return $read;
}

但它不会返回任何内容,它只是等待获取远程文件完成。

4

3 回答 3

9

试试这个,它会使用 curl 来获取文件的总大小,然后下载文件的部分块代理它给用户,所以没有等待curl 先下载它,我用 avi,mp4,mp3 测试了这个和一个exe,希望它有帮助:

<?php
$file = 'http://example.com/somefile.mp3';
download($file,2000);

/*
Set Headers
Get total size of file
Then loop through the total size incrementing a chunck size
*/
function download($file,$chunks){
    set_time_limit(0);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename='.basename($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    $size = get_size($file);
    header('Content-Length: '.$size);

    $i = 0;
    while($i<=$size){
        //Output the chunk
        get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
        $i = ($i+$chunks);
    }

}

//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
function chunk($ch, $str) {
    print($str);
    return strlen($str);
}

//Function to get a range of bytes from the remote file
function get_chunk($file,$start,$end){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
    $result = curl_exec($ch);
    curl_close($ch);
}

//Get total size of file
function get_size($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    return intval($size);
}
?>
于 2012-06-12T08:36:21.407 回答
2

尝试像这样使用 curl:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);

$msg = new HttpMessage($result);
return $msg->getBody();

然后返回值是请求文件的内容,然后您可以将其输出。所以不需要回调。HTTPMessage 文档

于 2012-06-12T06:48:59.833 回答
0

如何获取(8GB)远程文件并同时强制下载,同时不杀死你的服务器:

<?php

$url = 'http://www.example.com/bigfile.mp4';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($url).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');

readfile($url);

readfile()函数将分块读取流,并将其打印到输出,因此它不会使用太多内存...

于 2020-12-06T20:58:31.787 回答