Force download a file using PHP

A force-download script can give you more control over a file download than you would have providing a direct link that may be the good thing most of the time to read it online. But sometimes it can be very frustrating to click on a link to a document you want people to read, and then have to wait for it to download and finally open on your computer that may not have the right program to open it.

But what if you have a file you want people to download ? If it is an HTML file or a PDF file or an image file or a video file, you cannot just post link, as the web browsers open those automatically; instead you need to do some tricky thing using PHP.

PHP allows you to change the HTTP headers of files that you are writing, so that you can force a file to be downloaded that normally the browser would load in the same window. This is perfect for files like PDFs, document files, images, and video that you want your customers to download rather than read online.
If you have any query please feel free to send a query.


PHP 5.4
MySQL 5.5
Apace http Server 2.4
Netbeans 7.4 (Optional)

Step 1. Creates a php projects using Netbeans or create a project directory under htdocs.

Step 2. Create a php file called config.php with the below source code in the project directory

Step 3. Create a common.php file with below source code in the project directory. Please go through tutorial for $mimes array

Step 4. Create a php web page download.php with the following source code

Step 5. Run the php file download.php. You will get the file opened in new window for “Open with” or “Save” options.

That’s all. Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.