Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 91615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:03:28+00:00 2026-05-10T23:03:28+00:00

I’ve written a PHP script that handles file downloads, determining which file is being

  • 0

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).

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 "Content-type" 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?

Currently I’m only setting the value "Content-disposition: attachment; filename=arandomf.ile"

Update: I followed this guide here to build a more robust process for file downloads (http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/), 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?

Here’s my implementation:

/**  * 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' => 'application/pdf',         'txt' => 'text/plain',         'html' => 'text/html',         'exe' => 'application/octet-stream',         'zip' => 'application/zip',         'doc' => 'application/msword',         'xls' => 'application/vnd.ms-excel',         'ppt' => 'application/vnd.ms-powerpoint',         'gif' => 'image/gif',         'png' => 'image/png',         'jpeg' => 'image/jpg',         'jpg' => 'image/jpg',         'php' => '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="' . $fileName . '"');     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) && !connection_aborted() && $bytesSend < $newLength) {                 $buffer = fread($file, $chunkSize);                 echo $buffer;                 flush();                 $bytesSend += strlen($buffer);             }                          fclose($file);         }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:03:28+00:00Added an answer on May 10, 2026 at 11:03 pm

    Acoording to RFC 2046 (Multipurpose Internet Mail Extensions):

    The recommended action for an implementation that receives an
    ‘application/octet-stream’ entity is to simply offer to put the data in a file

    So I’d go for that one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Stick with your datatemplates for simple views with no code… May 12, 2026 at 5:39 am
  • Editorial Team
    Editorial Team added an answer Unable to replicate. I suspect that you're misrepresenting — oversimplifying,… May 12, 2026 at 5:39 am
  • Editorial Team
    Editorial Team added an answer Your need to set up a PHP or ASP Redirect… May 12, 2026 at 5:39 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.