与readfile相比,下载速度要慢很多 [英] fread a lot slower for downloads than readfile

查看:147
本文介绍了与readfile相比,下载速度要慢很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过PHP脚本向用户提供从URL下载的内容。当我使用 readfile()时,我得到我的连接可以支持的最大下载速度(约2.5MB / s),但是当我使用 fopen,fread, fclose 路由下载速度非常非常慢(约1-2KB / s)。



这是我的代码:

  header('Content-Description:File Transfer'); 
header('Content-Type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。$ filename);
header('Content-Transfer-Encoding:binary');
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
header('Content-Length:'。$ filesize);
ob_clean();
flush();

$ file = fopen($ url,'rb');

while(!feof($ file)){
echo fread($ file,2014);
}

,readfile代码只是 readfile($ link );



我不能只使用 readfile()函数,因为有两个原因,一个是我想限制用户下载速度(我可以通过读取这么多的数据来实现 fread ),我也想跟踪多少用户正在下载(我可以使用 readfile(),但不计入部分下载)。



有人知道为什么会发生这种情况吗?据我所知 readfile()只是一个 fopen,fread和fclose 的包装器,所以我不会



编辑:结束了cURL。

  $ curl = curl_init(); 
$ options = array(
CURLOPT_URL => $ rdLink,
CURLOPT_FAILONERROR => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_WRITEFUNCTION =>'readResponse'
);
curl_setopt_array($ curl,$ options);

if(!curl_exec($ curl)){
header('Location:http://whatever.com');
退出;
}
curl_close($ curl);

函数readResponse($ ch,$ data){
$ length = mb_strlen($ data,'8bit');

echo $ data;

return $ length;
}


解决方案

使用 stream_context_create() stream_get_contents()

  $ context = stream_context_create(); 
$ file = fopen($ url,'rb',FALSE,$ context);
while(!feof($ file))
{
// usleep(1000000);
echo stream_get_contents($ file,2014);
}

您还可以尝试使文件读取长度更大并放入usleep )缓慢执行。
似乎对于最新版本的PHP,似乎推荐使用流功能。
您可能还需要在fread()或stream_get_contents()前面添加一个@来抑制任何错误,至少在生产中。没有它,有点不幸,你有一个损坏的文件。


I'm serving downloads from a URL to my users through a PHP script. When using readfile() I get the maximum download speed my connection can support (about 2.5MB/s) however when I use the fopen, fread, fclose route the download speed is very, very slow (about 1-2KB/s).

Here's my code:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $filesize);
ob_clean();
flush();

$file = fopen($url, 'rb');

while(!feof($file)) {
    echo fread($file, 2014);
}

and the readfile code is simply readfile($link);.

I can't just use the readfile() function because of two reasons, one is I want to restrict the users download speed (which I can do with fread by only reading so much data) and I also want to track how much a user is downloading (I can do this with readfile() but it doesn't count partial downloads).

Does anyone know why this might be happening or how I can fix it? As far as I know readfile() is just a wrapper for fopen, fread and fclose so I don't get what's going wrong.

Edit: Ended up going with cURL for this.

$curl = curl_init();
$options = array(
    CURLOPT_URL => $rdLink,
    CURLOPT_FAILONERROR => true,
    CURLOPT_BINARYTRANSFER => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_WRITEFUNCTION => 'readResponse'
);
curl_setopt_array($curl, $options);

if(!curl_exec($curl)) {
    header('Location: http://whatever.com');
    exit;
}
curl_close($curl);

function readResponse($ch, $data) {
    $length = mb_strlen($data, '8bit');

    echo $data;

    return $length;
}

解决方案

Use stream_context_create() and stream_get_contents()

$context = stream_context_create();
$file = fopen($url, 'rb', FALSE, $context);
while(!feof($file))
{
    //usleep(1000000);
    echo stream_get_contents($file, 2014);
}

You could also try making the file read length larger and putting in a usleep() to slow execution. The stream functions seem to be recommended over fread for the latest version of PHP anyway. You might want to also prepend an @ in front of the fread() or stream_get_contents() to suppress any errors, at least in production. Without it, and a little mishap, and you have a corrupted file.

这篇关于与readfile相比,下载速度要慢很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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