$ _SERVER ['HTTP_RANGE']每次返回Null吗? [英] $_SERVER['HTTP_RANGE'] returns Null every time?
问题描述
在给定的代码中,其他条件每次都在运行.我已经使用了pdf.js库,并试图将pdf分成几部分.我在服务器端使用了此代码,但似乎只有其他条件正在运行,并且isset($_SERVER['HTTP_RANGE']
可能返回null
.因此,pdf会自动加载.
In the given code only else condition is running every time. I have used pdf.js library and trying to break the pdf in chunks. I user this code on server side but it seems like only else condition is running and perhaps isset($_SERVER['HTTP_RANGE']
returns null
. So the pdf gets loaded in the first go itself .
if(isset($_SERVER['HTTP_RANGE'])) {
$fp = @fopen($filepath, 'rb');
$size = filesize($filepath); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
// Extract the range string
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
// Make sure the client hasn't sent us a multibyte range
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
// (?) Echo some info to the client?
exit;
}
if ($range == '-') {
// The n-number of the last bytes is requested
$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;
// Validate the requested range and return an error if it's not correct.
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");
// (?) Echo some info to the client?
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1; // Calculate new content length
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
// Notify the client the byte range we'll be outputting
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
// Start buffered download
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
// In case we're only outputtin a chunk, make sure we don't
// read past the length
$buffer = $end - $p + 1;
}
set_time_limit(0); // Reset time limit for big files
echo fread($fp, $buffer);
flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);
}
else {
header("Content-Length: ".filesize($filepath));
header("X-Sendfile: $filepath");
readfile($filepath);
}
推荐答案
http://pdf.yt/足以发布源代码-它的旧版本是用PHP编写的,并由PDF.js提供支持.参见 https://github.com/joepie91/pdfy/blob/master/public_html/modules/download.php 以获得完整的解决方案.几乎是您上面的代码,带有正确的HTTP标头(例如接受范围:字节")和很少的解决方法.
Author of http://pdf.yt/ was kind enough to publish source code -- old version of which was written in PHP and it's powered by PDF.js. See https://github.com/joepie91/pdfy/blob/master/public_html/modules/download.php for complete solution. Pretty much it's your code above with proper HTTP headers (e.g. "Accept-Ranges: bytes") and few workarounds.
这篇关于$ _SERVER ['HTTP_RANGE']每次返回Null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!