使用PHP对大文件进行流式处理
我有一个200MB的文件,我想通过下载给用户。但是,由于我们希望用户只下载此文件一次,因此我们正在执行以下操作:使用PHP对大文件进行流式处理,php,download,Php,Download,我有一个200MB的文件,我想通过下载给用户。但是,由于我们希望用户只下载此文件一次,因此我们正在执行以下操作: echo file_get_contents('http://some.secret.location.com/secretfolder/the_file.tar.gz'); 强制下载。但是,这意味着整个文件必须加载到内存中,而内存通常不起作用。我们如何将此文件以每块kb的速度流式传输给他们?尝试类似的方法(源代码): 查看以下手册页中的示例: $fp=fsockopen(“www
echo file_get_contents('http://some.secret.location.com/secretfolder/the_file.tar.gz');
强制下载。但是,这意味着整个文件必须加载到内存中,而内存通常不起作用。我们如何将此文件以每块kb的速度流式传输给他们?尝试类似的方法(源代码):
查看以下手册页中的示例:
$fp=fsockopen(“www.example.com”,80,$errno,$errstr,30);
如果(!$fp){
回显“$errstr($errno)
\n”;
}否则{
$out=“GET/HTTP/1.1\r\n”;
$out.=“主机:www.example.com\r\n”;
$out.=“连接:关闭\r\n\r\n”;
fwrite($fp,$out);
而(!feof($fp)){
echo fgets(fp,128美元);
}
fclose($fp);
}
这将连接到www.example.com
,发送一个请求,然后获取并以128字节的块回显响应。您可能希望使其超过128字节。使用。顾名思义,它在发送之前不会将整个文件读入内存,而是直接将其输出到客户端
根据示例修改:
我在
这对我来说非常有效
<?php
class VideoStream
{
private $path = "";
private $stream = "";
private $buffer = 102400;
private $start = -1;
private $end = -1;
private $size = 0;
function __construct($filePath)
{
$this->path = $filePath;
}
/**
* Open stream
*/
private function open()
{
if (!($this->stream = fopen($this->path, 'rb'))) {
die('Could not open stream for reading');
}
}
/**
* Set proper header to serve the video content
*/
private function setHeader()
{
ob_get_clean();
header("Content-Type: video/mp4");
header("Cache-Control: max-age=2592000, public");
header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
$this->start = 0;
$this->size = filesize($this->path);
$this->end = $this->size - 1;
header("Accept-Ranges: 0-".$this->end);
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $this->start;
$c_end = $this->end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
if ($range == '-') {
$c_start = $this->size - substr($range, 1);
}else{
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
}
$c_end = ($c_end > $this->end) ? $this->end : $c_end;
if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
$this->start = $c_start;
$this->end = $c_end;
$length = $this->end - $this->start + 1;
fseek($this->stream, $this->start);
header('HTTP/1.1 206 Partial Content');
header("Content-Length: ".$length);
header("Content-Range: bytes $this->start-$this->end/".$this->size);
}
else
{
header("Content-Length: ".$this->size);
}
}
/**
* close curretly opened stream
*/
private function end()
{
fclose($this->stream);
exit;
}
/**
* perform the streaming of calculated range
*/
private function stream()
{
$i = $this->start;
set_time_limit(0);
while(!feof($this->stream) && $i <= $this->end) {
$bytesToRead = $this->buffer;
if(($i+$bytesToRead) > $this->end) {
$bytesToRead = $this->end - $i + 1;
}
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
$i += $bytesToRead;
}
}
/**
* Start streaming video content
*/
function start()
{
$this->open();
$this->setHeader();
$this->stream();
$this->end();
}
}
我使用readfile()强制下载时也遇到了这个问题。内存问题不在readfile中,而是在输出缓冲中
只要确保在读取文件之前关闭输出缓冲,问题就会得到解决
if (ob_get_level()) ob_end_clean();
readfile($yourfile);
适用于大小远大于分配内存限制的文件。如何传输文件。。。?尤其是当文件位于webroot之外时,该解决方案将不起作用。如果我错了,请纠正我,但使用原始套接字会迫使您自己实现您面临的所有HTTP功能,例如重定向、压缩、加密、分块编码。。。它可能在特定的场景下工作,但它不是最好的通用解决方案。块也可以gzip吗<代码>ini_集('zlib.output_compression','On')
此流是否会以用户可以在其被中断时恢复下载的方式进行?行$status=fclose($handle)代码>使我的浏览器在从下载标记调用脚本url时重定向到脚本url。这会阻止下载。在我的例子中,由于文件句柄会自动关闭,所以我省略了这一点,并且一切正常。我很好奇是否有人知道原因。如果此流被中断,用户会以某种方式继续下载吗?@Bludream不是当前形式。要处理下载的恢复,您应该检查客户端标题范围
。如果存在字节值,则可以在文件上使用fseek()
,并在发送前发送适当的内容范围
标题。正如原始问题所述,我不建议将其用于大型文件。较小的文件可以正常工作,但是我在一个大文件上使用fpassthru,我的下载失败了,因为“允许的内存大小已耗尽”。@magnic Yes,正如fpassthru
的手册所说,它会将文件输出到输出缓冲区。但是如果您首先调用ob\u end\u flush()
,那么输出缓冲将被禁用,这样您就不会达到内存限制:-DUsestream\u copy\u to\u stream(fopen('file.ext','rb')),STDOUT)
将流传输到STDOUT。如果您的默认缓冲区大小需要调整,请使用stream\u set\u chunk\u size($fp,$size)
<?php
// the file you want to send
$path = "path/to/file";
// the file name of the download, change this if needed
$public_name = basename($path);
// get the file's mime type to send the correct content type header
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $path);
// send the headers
header("Content-Disposition: attachment; filename=$public_name;");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($path));
// stream the file
$fp = fopen($path, 'rb');
fpassthru($fp);
exit;
<?php
class VideoStream
{
private $path = "";
private $stream = "";
private $buffer = 102400;
private $start = -1;
private $end = -1;
private $size = 0;
function __construct($filePath)
{
$this->path = $filePath;
}
/**
* Open stream
*/
private function open()
{
if (!($this->stream = fopen($this->path, 'rb'))) {
die('Could not open stream for reading');
}
}
/**
* Set proper header to serve the video content
*/
private function setHeader()
{
ob_get_clean();
header("Content-Type: video/mp4");
header("Cache-Control: max-age=2592000, public");
header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
$this->start = 0;
$this->size = filesize($this->path);
$this->end = $this->size - 1;
header("Accept-Ranges: 0-".$this->end);
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $this->start;
$c_end = $this->end;
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
if ($range == '-') {
$c_start = $this->size - substr($range, 1);
}else{
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
}
$c_end = ($c_end > $this->end) ? $this->end : $c_end;
if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $this->start-$this->end/$this->size");
exit;
}
$this->start = $c_start;
$this->end = $c_end;
$length = $this->end - $this->start + 1;
fseek($this->stream, $this->start);
header('HTTP/1.1 206 Partial Content');
header("Content-Length: ".$length);
header("Content-Range: bytes $this->start-$this->end/".$this->size);
}
else
{
header("Content-Length: ".$this->size);
}
}
/**
* close curretly opened stream
*/
private function end()
{
fclose($this->stream);
exit;
}
/**
* perform the streaming of calculated range
*/
private function stream()
{
$i = $this->start;
set_time_limit(0);
while(!feof($this->stream) && $i <= $this->end) {
$bytesToRead = $this->buffer;
if(($i+$bytesToRead) > $this->end) {
$bytesToRead = $this->end - $i + 1;
}
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
$i += $bytesToRead;
}
}
/**
* Start streaming video content
*/
function start()
{
$this->open();
$this->setHeader();
$this->stream();
$this->end();
}
}
$stream = new VideoStream($filePath);
$stream->start();
if (ob_get_level()) ob_end_clean();
readfile($yourfile);