How to Use PHP to Force a File Download

Laptop computer with high speed connection.(Digital composite)
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.

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.
    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");
  6. Point to the file you want to download:
    readfile("huge_document.pdf");
  7. Then close the PHP block and save the file:
    ?>
  1. 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");
    ?>
  2. 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

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/htmland your file won’t download.

Format
mla apa chicago
Your Citation
Kyrnin, Jennifer. "How to Use PHP to Force a File Download." ThoughtCo, Dec. 12, 2017, thoughtco.com/using-php-to-force-download-3469180. Kyrnin, Jennifer. (2017, December 12). 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 December 13, 2017).