How To Use PHP to Force a File Download

Get Browsers to Download a File They Would Normally Open Inline

Format
mla apa chicago
Your Citation
Kyrnin, Jennifer. "How To Use PHP to Force a File Download." ThoughtCo, Jan. 3, 2017, thoughtco.com/using-php-to-force-download-3469180. Kyrnin, Jennifer. (2017, January 3). How To Use PHP to Force a File Download. Retrieved from https://www.thoughtco.com/using-php-to-force-download-3469180 Kyrnin, Jennifer. "How To Use PHP to Force a File Download." ThoughtCo. https://www.thoughtco.com/using-php-to-force-download-3469180 (accessed October 24, 2017).
Laptop computer with high speed connection.(Digital composite)
Laptop computer with high speed connection. Image courtesy Digital Vision / 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.

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 link to that document because (as we have just covered) a web browser opens those document automatically and display the 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.

Read to learn how to make this happen? Here's the step by step instructions.

Difficulty: Hard

Time Required: 10 minutes

What You Need:

Here’s How to Do This:

  1. Upload the file you want to make available for download to your web server. For example,say you have as 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.
    huge_document.pdf
  2. Edit a new PHP file in your web editor—for ease of use, I recommend naming it the same name as your downloaded file, only with the extension .php. For example:
    huge_document.php
  3. Open the PHP block in your document:
    <?php
  4. On the next line, set the HTTP header:
    header("Content-disposition: attachment; filename=huge_document.pdf");
  5. Then set the MIME-type of the file:
    header("Content-type: application/pdf");
  1. Point to the file you want to download:
    readfile("huge_document.pdf");
  2. Then close the PHP block and save the file:
    ?>
  3. Your PHP file should look like this:
    <?php
    header("Content-disposition: attachment; filename=huge_document.pdf");
    header("Content-type: application/pdf");
    readfile("huge_document.pdf");
    ?>
  4. Link to your PHP file as a download link from a webpage. For example:
    <a href="huge_document.php">Download my huge document (PDF)</a>

Tips:

  • 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 text/html and your file won’t download.

Original article by Jennifer Krynin. Edited by Jeremy Girard on 01/03/17

Format
mla apa chicago
Your Citation
Kyrnin, Jennifer. "How To Use PHP to Force a File Download." ThoughtCo, Jan. 3, 2017, thoughtco.com/using-php-to-force-download-3469180. Kyrnin, Jennifer. (2017, January 3). How To Use PHP to Force a File Download. Retrieved from https://www.thoughtco.com/using-php-to-force-download-3469180 Kyrnin, Jennifer. "How To Use PHP to Force a File Download." ThoughtCo. https://www.thoughtco.com/using-php-to-force-download-3469180 (accessed October 24, 2017).