Detect MIME type and buffer it to browser using PHP

The following piece of code handles detection of mime type, file size and existence of the file for you. I always use this piece for buffering files into browser of my users.

         if (file_exists($fileLocation)) {
             header('Content-Description: File Transfer');
             header('Content-Type: ' .mime_content_type($fileLocation));
             header('Content-Disposition: attachment; filename="'.basename($fileLocation).'"');
             header('Expires: 0');
             header('Cache-Control: must-revalidate');
             header('Pragma: public');
             header('Content-Length: ' . filesize($fileLocation));

All you have to do is to change the variable of $filelocation with your own variable which contains the full address of your local file.

Hope it helps some of you out there.

Leave a Reply