使用HTTP接受范围来提供大文件时,为什么不满足PHP的“ memory_limit”和“ max_execution_time”限制?

问题描述

以下脚本用于(评估)将大型视频文件发送到客户端。它在后面使用http标头Accept-Ranges。即使在处理大文件(> 2 GB)时,也没有满足PHP的限制(为进行测试,我设置了较小的值,例如memory_limit = 16MB和max_execution_time = 30)。

我想“理解”后面的上下文,因为chrome仅显示一个(部分)请求,每隔几秒钟就会增加“时间”和“大小”,尽管apache日志文件中没有显示其他请求。

$file = './videos/' . basename($_GET['video']);

if(!file_exists($file)) return; 

$fp = @fopen($file,'rb');      
$size   = filesize($file); // File size 
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte  

header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
header("Accept-Ranges: bytes"); 

if (isset($_SERVER['HTTP_RANGE'])) {
    $c_start = $start;              
    $c_end   = $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 $start-$end/$size");
        exit;      
    }              
    if ($range == '-') {            
        $c_start = $size - substr($range,1);
    }else{         
        $range  = explode('-',$range); 
        $c_start = $range[0];           
        $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { 
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;      
    }
    $start  = $c_start;             
    $end    = $c_end;               
    $length = $end - $start + 1;    
    fseek($fp,$start);             
    header('HTTP/1.1 206 Partial Content');
}   
               
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);

$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) { 
    if ($p + $buffer > $end) {      
        $buffer = $end - $p + 1;        
    }              
    set_time_limit(0);              
    echo fread($fp,$buffer);       
    ob_flush();    
}
fclose($fp);       
exit();

解决方法

通过curl在没有max_execution_time=1并提供2.893 GB文件的情况下使用% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 23 2893M 23 678M 0 0 92.5M 0 0:00:31 0:00:07 0:00:24 0 curl: (18) transfer closed with 2322893144 bytes remaining to read 通过命令行请求脚本来找出更多细节。

在同一台计算机上(7秒和678 MB的中断):

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed  
20 2893M   20  599M    0     0  12.1M      0  0:03:58  0:00:49  0:03:09 10284 
curl: (18) transfer closed with 2404747608 bytes remaining to read

在带宽较低的外国机器上(49秒和599 MB的中断):

max_execution_time=30

关于这一点,max_execution_time=1在这种情况下并不是真正的“小”,因为它可以传输大约30 * 600 MB(= 18 GB!)。客户端的请求持续时间与服务器端的执行时间完全无关。

确实,当在<i class="zmdi zmdi-close"></i> <i class="fac fac-search"></i> <i aria-hidden="true" class="flaticon flaticon-social-media"></i> <i aria-hidden="true" class="flaticon flaticon-report"></i> 上从html5视频对象中调用脚本时,有几个部分请求完成(在带宽更低的桌面客户端上,每个请求大约需要14分钟,直到中止并转移大约500 MB)。

最后,我只是感到困惑,因为客户端请求持续时间和服务器端执行时间之间有如此之大的差异。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...