How to Use PHP to Force a File Download

Fibre optic broadband
John Lamb / Getty Images

When you think about it, web browsers are amazing feats of complex programming. They are tools that are a part of our everyday life - used for everything from checking on the status of friends and family, to communicating with those people, to making purchases, to watching videos, to taking care of our financial lives, and so much more. As prevalent as browsers are in our lives, the reality is that most people don’t appreciate how useful they really are.

Behind the Scenes

One thing that browsers do behind the scenes is try to make all the clicks that a person does during a browsing session actually do something. This means that more and more file types can be opened for viewing directly in the web browsers.

Most of the time, this is a good thing, as it can be very frustrating to click on a link to a document you want to read and then have to wait for it to download and finally open on your computer. That frustration reaches next level when you wait for this download, only to discover that you don’t have the right program to open the document. These days, that rarely happens because browsers do, indeed, display the document directly inline. For example, PDF files do not download by default. Instead, they display directly in the web browser similar to how a web page would display.

What if you have a file you want people to download instead of viewing it directly in the web browser? If it is an HTML file or a PDF, you can’t just post a link to that document because (as we have just covered) a web browser opens those documents automatically and displays them inline. To make these files download to a person's computer, you instead need to do some trickery using PHP.

PHP allows you to change the HTTP headers of files that you’re writing. This process makes it 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 videos that you want your customers to download rather than consume online directly from the browser.

You'll need PHP on the web server where your files will be hosted, a file to be downloaded, and the MIME type of the file in question.

How to Do This

  1. Upload the file you want to make available for download to your web server. For example, say you have a PDF file that you want people to download when they click a link. You would first upload that file to your website's hosting environment.
  2. Edit a new PHP file in your web editor – for ease of use, we recommend naming it the same name as your downloaded file, only with the extension .php. For example:
  3. Open the PHP block in your document:
  4. On the next line, set the HTTP header:
    1. header("Content-disposition: attachment; filename=huge_document.pdf");
  5. Then set the MIME-type of the file:
    header("Content-type: application/pdf");
  6. Point to the file you want to download:
  7. Then close the PHP block and save the file:
  8. Your PHP file should look like this:
    1. <?php
  9. header("Content-disposition: attachment; filename=huge_document.pdf");
  10. header("Content-type: application/pdf");
  11. readfile("huge_document.pdf");
  12. ?>
  13. Link to your PHP file as a download link from a webpage. For example:
    1. <a href="huge_document.php">Download my huge document (PDF)</a

    There should be no spaces or carriage returns anywhere in the file (except after a semi-colon). Blank lines will cause PHP to default to the MIME type

    and your file won’t download.