PHP codes to tell browsers to open the download dialog box for users to download a file

Ideally, when we build web applications which generate files on demand, we will want our users to be able to use their browsers to save a copy of our files in their local harddisk. For instance, the tool for generating customized codes for countries in the world, which I had created earlier, will tell browsers to prompt a download dialog box after users had submitted the form.

Image of firefox dialog box after submitting request to generate custom codes from list of countries in the world.

However, this is not the default behavior of browsers. By default, browsers will show the contents of a HTTP response generated by our PHP script in the browser window. This behaviour can be seen when I point my browser to http://www.techcoil.com/robots.txt.

In this post, I will discuss how we can tell browsers to show a dialog box for users to save the contents generated by a PHP script as a file.

Some information about the HTTP response

When our PHP script is executed, it will mean that a HTTP request was sent to our web server and that the browser is waiting for a HTTP response from our PHP script.

A HTTP response has two distinct portions:

  1. The meta data portion to tell the browser whether the request is successful, what content to expect, how to deal with the content and etc. Such data had to be sent to the HTTP client before the actual content.
  2. The contents portion which is what our HTTP client had requested for. This portion can be blank, for example, when a HTTP response with a 404 status code is specified in the meta data section. If not blank, this portion is where our PHP script writes what the HTTP client wants, for example, a HTML document, a photo, a text document and etc.

Telling browsers to download robots.txt, instead of just opening it right away

Extending from the robot.txt example, I will create a separate PHP script on my web server to tell browsers to show their users a download dialog box to download the same robot.txt file.

<?php
// Specify that our robots.txt is text document.
header('Content-type: plain/text');
// Tell browsers to open up the download dialog box to download
// our robots.txt. In addition, the browser should default the 
// file name to robots.txt
header('Content-Disposition: attachment; filename="robots.txt"');

// Write the contents of robots.txt out to the browser
readfile(dirname(dirname(__FILE__)) . '/robots.txt');
?>

The script consists of three main lines of codes. We first specify some meta data for our browser via the header function:

  • The type of content to expect, via Content-Type. We tell browsers that robots.txt is text data.
  • The way in which the content is to be presented to the user, via Content-Disposition. We specify that we want our browser to present the content as a file attachment to the user (thereby showing a dialog box) and to suggest the filename as robots.txt.

After our PHP script provide the browser with the necessary meta information about the content, it proceeds to send out the contents of the file. To do so, it first locate the actual file by using the dirname function and the __FILE__ magic constant. It then reads the content of the file via the file_get_contents function and writes the content of the file via the print function.

Related posts

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.