PHP Curl直接下载/流式部分内容

我想将文件从其他网站/ Webadress直接发送/流式传输给用户,而不是将文件存储在我的身上 服务器。 文件存档大约100MB到1GB </ p>

使用一个线程的下载工作正常。 这是我的代码:</ p>

  $ ch5 = curl_init(); 
if(isset($ _ SERVER ['HTTP_RANGE'])){
// http:// stackoverflow .com / questions / 157318 / resumable-downloads-when-using-php-to-send-the-file
preg_match('/ bytes =(\ d +) - (\ d +)?/',$ _SERVER ['HTTP_RANGE '],$ matches);
$ offset = intval($ matches [1]);
$ end = $ matches [2] || $ matches [2] ==='0'? intval($ matches [2]):$ size - 1;
curl_setopt($ ch5,CURLOPT_RANGE,“$ offset- $ end”);
}
ncurl_setopt($ ch5,CURLOPT_URL,$ direct_link);
curl_setopt( $ ch5,CURLOPT_TIMEOUT,3550);

curl_setopt($ ch5,CURLOPT_CONNECTTIMEOUT,15);

curl_setopt($ ch5,CURLOPT_HEADERFUNCTION,'readHeader');
curl_exec($ ch5);
$ dlsize = curl_getinfo($ ch5,CURLINFO_SIZE_DOWNLOAD);
$ time = curl_getinfo($ ch5,CURLINFO_TOTAL_TIME);
curl_close($ ch5);

_函数readHeader($ ch,$ header)//将标题从Downloaded文件发送到User
{
标题($ header);
返回strlen($ header);
} \ n </ code> </ pre>

问题是PHP如果请求包含类似“Range:bytes = 0-”的范围并将其发送给用户,则下载完整文件。 但用户下载速度慢得多(导致下载速度变慢),然后将其下载到我的服务器,但如果文件已完全下载,则该文件在外部服务器上不可用。
当客户端为第二个文件发出范围请求时 请求转到外部服务器的一半或其他内容,文件不可用,因为它已被第一个请求完全下载,但我无法访问数据或有没有办法获取这些数据,而不保存它们 我的磁盘? </ p>

我想保存带宽,以便让用户可以进行范围请求。

因此,PHP应该只下载用户真正想要下载的数量,或者在外部服务器和用户之间建立直接连接。</ p>

实际上我使用的是nginx / 1.6.2和PHP 5.5。 Ubuntu 12.04.5上的20-1 </ p>
</ div>

展开原文

原文

I want to send/stream Files from an other Website/Webadress directly to the user, without storing the Files on my Server. The Files are Archives about 100MB to 1GB

The download with one thread works fine. Here is my Code:

$ch5 = curl_init();
if (isset($_SERVER['HTTP_RANGE'])) {
    //http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file
    preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
    $offset = intval($matches[1]);
    $end = $matches[2] || $matches[2] === '0' ? intval($matches[2]) : $size - 1;
    curl_setopt($ch5, CURLOPT_RANGE, "$offset-$end");
}
curl_setopt($ch5, CURLOPT_URL, $direct_link);
curl_setopt($ch5, CURLOPT_TIMEOUT, 3550); 
curl_setopt($ch5, CURLOPT_CONNECTTIMEOUT, 15); 
curl_setopt($ch5, CURLOPT_HEADERFUNCTION, 'readHeader');
curl_exec($ch5);
$dlsize = curl_getinfo($ch5,CURLINFO_SIZE_DOWNLOAD );
$time = curl_getinfo($ch5,CURLINFO_TOTAL_TIME);
curl_close($ch5);

function readHeader($ch, $header) // Send header from the Downloaded file to the User
{
    header($header);
    return strlen($header);
}

The problem is that PHP Download the complete file if the request contains an Range like "Range: bytes=0-" and sending it to the user. But the User download much slower(Cause slower download speed) then downloading it to my Server, but if the file is completely downloaded, the file isn't available on the external Server. And when the Client make an Range Request for the second half or something else the Request goes to the external Server and the file isn't available, cause it is already completely downloaded by the first request, but I cant access the Data or is there a way to get these Data, without saving them on my disk?

I want to save Bandwidth an want to give users the possibility to make Range Requests. So PHP should only download the amount the user really want to download or make a direct connection between the external Server and User.

Actually I am using nginx/1.6.2 and PHP 5.5.20-1 on Ubuntu 12.04.5

dongqiaogouk86049
dongqiaogouk86049 我通过使用带有X-Sendfile的nginx解决了我的问题。更多信息:wiki.nginx.org/XSendfile
5 年多之前 回复
dongxietao0263
dongxietao0263 Squid有这些功能。
接近 6 年之前 回复
doushun4666
doushun4666 问题是,用户不应该看到原始URL,我需要PHP来存储Downloaded大小,因为每个用户都有一个有限的下载队伍。或者是否有可能在PHP中使用Squid
接近 6 年之前 回复
douchao1879
douchao1879 PHP不是你想要用的。看看使用Squidsquid-cache.org
接近 6 年之前 回复

1个回答



我通过使用带有X-Sendfile / X-Accel-Redirect的nginx解决了我的问题。 更多信息:wiki.nginx.org/XSendfile </ p>
</ div>

展开原文

原文

I solved my problem by using nginx with X-Sendfile/X-Accel-Redirect . More Information here: wiki.nginx.org/XSendfile

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐