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.
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:
- 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.
- 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
I name this script as
downloadRobot.txt.php and place it in the
poc folder, thereby making it accessible via http://www.techcoil.com/poc/downloadRobots.txt.php.
<?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
- 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
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
- Downloading a file via HTTP post and HTTP get in C#
- Sending a file and some form data via HTTP post in C#
- The HTTP response and how it relates to System.Net.HttpListener
- Code segments for rendering html 4.0 pages
- Generate PHP codes with PHP