php - 多次刷新页面时,fastCGI 崩溃并出现 500 错误
问题描述
我使用以下代码在 localhost(窗口 IIS)中使用 PHP 流式传输 mp4 文件。当我多次单击搜索栏或频繁刷新页面时,页面崩溃并出现错误 500:
FastCGI 进程最近经常失败。请稍后再试请求
<?php
$file = 'test.mp4';
$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);
flush();
}
fclose($fp);
exit();
?>
我不确定这是资源消费者代码还是 fastCGI 模块有限制还是什么?任何帮助,将不胜感激。
解决方案
推荐阅读
- selenium - 我正在使用 selenium 远程驱动程序 3.141.59.jar 和 guava-25.0-jre.jar 但我收到如下所述的错误
- tinymce - TinyMCE 编辑器是否支持 webp 类型的图片上传?
- joomla - 即使在完成安装后,Joomla 4.0.3 也会重新启动安装程序
- swiftui - SwiftUI NavigationLink 控制台错误(SwiftUI 在推送导航链接时遇到问题。请提交错误。)
- echarts - 连接除y轴缩放以外的echarts
- xslt - For-Each 标签不基于 XSLT 1.0 中的两个单独的详细信息部分标签打印
- c# - 用于在特定字符集后返回电子邮件列表的正则表达式(c#)
- sql - 列出公司所有年份的年销售额
- ipc - 简单的共享内存 IPC 是否需要互斥锁?
- r - 根据 r 中的相似 ID 将一个表中的值添加到另一个表中的新列