Home PHP JavaScript CSS WordPress APIs .htaccess Other How-To Useful Scripts What I Recommend
Posted on by Aleksandar Gichevski ()
When coding our systems, we constantly need to give members the ability to download files from server, like their report files, pdf guides, excel documents and any other. Using PHP we can perform creating on specific files and execute automatic download. I have seen lot of complicated and dedicated classes doing this but in fact can be done pretty simple in just few lines of code. Most important thing to do when creating this script is adding the right headers to make sure download works in all browsers including IE.

Set Filename, Path to Download File and File Type

First what we are going to do is set the name of our new file, path to file we want to download and type of file (chosen from known available mime types). Note: in this script are added some of the most used MIME types. See here more about Available MIME Types
 "application/pdf",
     "txt" => "text/plain",
     "html" => "text/html",
     "htm" => "text/html",
    "exe" => "application/octet-stream",
    "zip" => "application/zip",
    "doc" => "application/msword",
    "xls" => "application/ms-excel",
    "ppt" => "application/ms-powerpoint",
    "gif" => "image/gif",
    "png" => "image/png",
    "jpeg"=> "image/jpg",
    "jpg" =>  "image/jpg",
    "php" => "text/plain"
 ); 
 
$mime_type = $mime_types[$file_type];

Add The Right Headers

To Download File with PHP we need to add just four headers that will do the job in all browsers. The first two are default for download file and the second two headers are added to make sure this script works also in IE6 and IE7, newer versions of IE don't use these headers so is not harm to them. Make sure you have quotes in the filename in case the file may contain spaces.


Disable Output Compression

Disabling Output Compression is important especially in IE because Content-Disposition may be ignored and download action can cause errors.


Here is full code for php download file:

 "application/pdf",
     "txt" => "text/plain",
     "html" => "text/html",
     "htm" => "text/html",
    "exe" => "application/octet-stream",
    "zip" => "application/zip",
    "doc" => "application/msword",
    "xls" => "application/ms-excel",
    "ppt" => "application/ms-powerpoint",
    "gif" => "image/gif",
    "png" => "image/png",
    "jpeg"=> "image/jpg",
    "jpg" =>  "image/jpg",
    "php" => "text/plain"
 ); 
 
$mime_type = $mime_types[$file_type];

//set headers
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-type: ".$mime_type); 
//for IE6 and IE7
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

// required in IE, or Content-Disposition may be ignored
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

readfile($file_path);

Other Posts You Might Like

Get a fast, free website audit


Enter your URL below to get full in-depth SEO report and tips.

‚Äč

Useful Scripts

Most Popular Posts

Recent Posts

June 14th, 2014
Website Optimization Using Gzip Compression

June 10th, 2014
Google Search AutoComplete API

May 14th, 2014
What is a Tag Cloud and How to Calculate it by Formula

March 27th, 2014
Error writing file /tmp (errcode 28) Solved!

February 15th, 2014
Fixed CSS & HTML Navigation Bar

February 9th, 2014
Benchmark Your Server (CPU, File IO, MySQL) with SysBench

February 8th, 2014
JavaScript Array Basics

January 29th, 2014
JVZoo IPN API in PHP

January 25th, 2014
Payoneer Debit Card for Freelancers to receive money online

January 24th, 2014
How HTML Color Codes are generated?

Read Latest Posts directly on Facebook

Archive