【问题标题】:$_SERVER['HTTP_RANGE'] returns Null every time?$_SERVER['HTTP_RANGE'] 每次都返回 Null?
【发布时间】:2016-05-22 03:33:14
【问题描述】:

在给定的代码中,只有 else 条件每次都在运行。我已经使用了 pdf.js 库并试图将 pdf 分成块。我在服务器端使用此代码,但似乎只有 else 条件正在运行,也许isset($_SERVER['HTTP_RANGE'] 返回null。所以 pdf 会在第一次加载时自行加载。

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);
}

【问题讨论】:

  • 此变量仅在客户端(“用户代理”)发出请求请求特定范围的字节(Accept-Ranges 请求标头)而不是整个文档时才存在。
  • @fusion3k 有没有办法可以发出范围请求而不是整个文档?
  • 它取决于客户端,而不是服务器
  • 脚本必须支持范围和非范围 GET 请求(可能还有 CORS 的 OPTIONS)。在这两种情况下,服务器都应返回“Accept-Ranges: bytes”,因此客户端会知道服务器脚本支持范围请求。

标签: php pdf pdf.js


【解决方案1】:

http://pdf.yt/ 的作者很乐意发布源代码——旧版本是用 PHP 编写的,它由 PDF.js 提供支持。有关完整解决方案,请参阅https://github.com/joepie91/pdfy/blob/master/public_html/modules/download.php。几乎是您上面的代码带有正确的 HTTP 标头(例如“Accept-Ranges: bytes”)和一些解决方法。

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多