$_SERVER['HTTP_RANGE'] returns Null every time?
<p>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 <code>isset($_SERVER['HTTP_RANGE']</code> returns <code>null</code>. So the pdf gets loaded in the first go itself .</p>

<pre><code>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]) &amp;&amp; is_numeric($range[1])) ? $range[1] : $size;
        }
        $c_end = ($c_end &gt; $end) ? $end : $c_end;
        // Validate the requested range and return an error if it's not correct.
        if ($c_start &gt; $c_end || $c_start &gt; $size - 1 || $c_end &gt;= $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) &amp;&amp; ($p = ftell($fp)) &lt;= $end) {

        if ($p + $buffer &gt; $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);
}
</code></pre>