Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Web =>  PHP

Output a file with HTTP range header in PHP

  sonic0002      2013-01-09 05:55:05      17,087    3

When downloading a large file, we may encounter some network issues which causes download termination and only part of the file is downloaded. When the network connection resumes next time, we may need to redownload the file again from the beginning. In order to save bandwidth, http provides a Range parameter in its header which can control the file transfer flow. With the range parameter in the header, we can resume the download from where we stop.

Here is a piece of PHP code snippet which uses the range header to control which part of the file to transfer:

      $extension = substr(strrchr($filename,'.'),1);
      if ($extension == "mp3") {
        $mimeType = "audio/mpeg";
      } else if ($extension == "ogg") {
        $mimeType = "audio/ogg";
      if (!file_exists($location))
        header ("HTTP/1.1 404 Not Found");
      $size  = filesize($location);
      $time  = date('r', filemtime($location));
      $fm = @fopen($location, 'rb');
      if (!$fm)
        header ("HTTP/1.1 505 Internal server error");
      $begin  = 0;
      $end  = $size - 1;
      if (isset($_SERVER['HTTP_RANGE']))
        if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
        $begin  = intval($matches[1]);
        if (!empty($matches[2]))
          $end  = intval($matches[2]);
      if (isset($_SERVER['HTTP_RANGE']))
        header('HTTP/1.1 206 Partial Content');
        header('HTTP/1.1 200 OK');
      header("Content-Type: $mimeType");
      header('Cache-Control: public, must-revalidate, max-age=0');
      header('Pragma: no-cache');  
      header('Accept-Ranges: bytes');
      header('Content-Length:' . (($end - $begin) + 1));
      if (isset($_SERVER['HTTP_RANGE']))
        header("Content-Range: bytes $begin-$end/$size");
      header("Content-Disposition: inline; filename=$filename");
      header("Content-Transfer-Encoding: binary");
      header("Last-Modified: $time");
      $cur  = $begin;
      fseek($fm, $begin, 0);
      while(!feof($fm) && $cur <= $end && (connection_status() == 0))
        print fread($fm, min(1024 * 16, ($end - $cur) + 1));
        $cur += 1024 * 16;

Here is the document of range :

The use of range : Usually it is used to resume file transfer process, also when you want to transfer a large file, you can use it to control partial file transfer to release the server IO pressure.

Source :



Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



codefinder [Reply]@ 2015-03-01 13:12:10

in line 38 there is one empty too much...

sonic0002 [Reply]@ 2015-03-02 04:17:12

Thakn you. Have corrected it.

reminoco [Reply]@ 2017-06-16 08:33:12

thank you for this article but i get    this error stream does not support seeking


Hosted with love by Github

By sonic0002
A cute footer seen on JSch's website We geeks are not so boring sometimes.