title: HTTP Headers for File Downloads
body: <p>I've written a PHP script that handles file downloads, determining which file is being requested and setting the proper HTTP headers to trigger the browser to actually download the file (rather than displaying it in the browser).</p>
  <p>I now have a problem where some users have reported certain files being identified incorrectly (so regardless of extension, the browser will consider it a GIF image). I'm guessing this is because I haven't set the &quot;Content-type&quot; in the response header. Is this most likely the case? If so, is there a fairly generic type that could be used for all files, rather than trying to account for every possible file type?</p>
  <p>Currently I'm only setting the value &quot;Content-disposition: attachment; filename=arandomf.ile&quot;</p>
  <p><strong>Update:</strong> I followed this guide here to build a more robust process for file downloads (<a href="http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/" rel="nofollow noreferrer">http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/</a>), but there is a significant delay between when the script is executed and when the browser's download dialog appears. Can anyone identify the bottleneck that is causing this?</p>
  <p>Here's my implementation:</p>
  <pre><code>/**
   * Outputs the specified file to the browser.
   *
   * @param string $filePath the path to the file to output
   * @param string $fileName the name of the file
   * @param string $mimeType the type of file
   */
  function outputFile($filePath, $fileName, $mimeType = '') {
      // Setup
      $mimeTypes = array(
          'pdf' =&gt; 'application/pdf',
          'txt' =&gt; 'text/plain',
          'html' =&gt; 'text/html',
          'exe' =&gt; 'application/octet-stream',
          'zip' =&gt; 'application/zip',
          'doc' =&gt; 'application/msword',
          'xls' =&gt; 'application/vnd.ms-excel',
          'ppt' =&gt; 'application/vnd.ms-powerpoint',
          'gif' =&gt; 'image/gif',
          'png' =&gt; 'image/png',
          'jpeg' =&gt; 'image/jpg',
          'jpg' =&gt; 'image/jpg',
          'php' =&gt; 'text/plain'
      );
      
      $fileSize = filesize($filePath);
      $fileName = rawurldecode($fileName);
      $fileExt = '';
      
      // Determine MIME Type
      if($mimeType == '') {
          $fileExt = strtolower(substr(strrchr($filePath, '.'), 1));
          
          if(array_key_exists($fileExt, $mimeTypes)) {
              $mimeType = $mimeTypes[$fileExt];
          }
          else {
              $mimeType = 'application/force-download';
          }
      }
      
      // Disable Output Buffering
      @ob_end_clean();
      
      // IE Required
      if(ini_get('zlib.output_compression')) {
          ini_set('zlib.output_compression', 'Off');
      }
      
      // Send Headers
      header('Content-Type: ' . $mimeType);
      header('Content-Disposition: attachment; filename=&quot;' . $fileName . '&quot;');
      header('Content-Transfer-Encoding: binary');
      header('Accept-Ranges: bytes');
      
      // Send Headers: Prevent Caching of File
      header('Cache-Control: private');
      header('Pragma: private');
      header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
      
      // Multipart-Download and Download Resuming Support
      if(isset($_SERVER['HTTP_RANGE'])) {
          list($a, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
          list($range) = explode(',', $range, 2);
          list($range, $rangeEnd) = explode('-', $range);
          
          $range = intval($range);
          
          if(!$rangeEnd) {
              $rangeEnd = $fileSize - 1;
          }
          else {
              $rangeEnd = intval($rangeEnd);
          }
          
          $newLength = $rangeEnd - $range + 1;
          
          // Send Headers
          header('HTTP/1.1 206 Partial Content');
          header('Content-Length: ' . $newLength);
          header('Content-Range: bytes ' . $range - $rangeEnd / $fileSize);
      }
      else {
          $newLength = $fileSize;
          header('Content-Length: ' . $fileSize);
      }
      
      // Output File
      $chunkSize = 1 * (1024*1024);
      $bytesSend = 0;
      
      if($file = fopen($filePath, 'r')) {
          if(isset($_SERVER['HTTP_RANGE'])) {
              fseek($file, $range);
              
              while(!feof($file) &amp;&amp; !connection_aborted() &amp;&amp; $bytesSend &lt; $newLength) {
                  $buffer = fread($file, $chunkSize);
                  echo $buffer;
                  flush();
                  $bytesSend += strlen($buffer);
              }
              
              fclose($file);
          }
      }
  }
  </code></pre>