curl 获取远程文件并同时强制下载 [英] curl get remote file and force download at same time

查看:45
本文介绍了curl 获取远程文件并同时强制下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取远程文件并同时强制将其下载给用户.贴不上代码,代码太长了.但是 curl 函数可以工作,但问题是它在获取远程文件之前不会输出任何内容,然后强制将其下载给用户.

I'm trying to get a remote file and force download it to user at the same time. I can't paste the code ,the code is too long . but the curl function works ,but the problem is it doesn't out put anything till it gets the remote file first then it force downloads it to user.

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

I use this to tell curl to return a callback

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

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

now in my readCallback function I do this :

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

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

but it doesn't return anything it just waits till fetching remote file is finished.

推荐答案

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

Try this, it will use curl to get the total size of the file then download partial chunks of the file proxying it to the user so as there is no wait for curl to download it first, I tested this with an avi,mp4,mp3 and an exe, hope it helps:

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

这篇关于curl 获取远程文件并同时强制下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆