{"id":230,"date":"2012-05-29T09:06:02","date_gmt":"2012-05-29T01:06:02","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=230"},"modified":"2018-09-03T22:13:30","modified_gmt":"2018-09-03T14:13:30","slug":"php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file\/","title":{"rendered":"PHP codes to tell browsers to open the download dialog box for users to download a file"},"content":{"rendered":"<p>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 <a href=\"http:\/\/www.techcoil.com\/blog\/handy-tools\/code-generator-for-list-of-countries\/\" title=\"Code generator for list of countries\" target=\"_blank\">tool for generating customized codes for countries in the world<\/a>, which I had created earlier, will tell browsers to prompt a download dialog box after users had submitted the form.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/firefox-open-or-save-file-dialogbox.jpg\" alt=\"Image of firefox dialog box after submitting request to generate custom codes from list of countries in the world.\"\/><\/p>\n<p>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 <a href=\"http:\/\/www.techcoil.com\/robots.txt\" title=\"Link to robots.txt\" target=\"_blank\">http:\/\/www.techcoil.com\/robots.txt<\/a>.<\/p>\n<p>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. <\/p>\n<h3>Some information about the HTTP response<\/h3>\n<p>When our PHP script is executed, it will mean that a <strong>HTTP request<\/strong> was sent to our <a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/a-mechanism-for-serving-http-requests-in-c\/\" title=\"A mechanism for serving HTTP requests in C#\" target=\"_blank\">web server<\/a> and that the browser is waiting for a <strong>HTTP response<\/strong> from our PHP script. <\/p>\n<p>A HTTP response has two distinct portions:<\/p>\n<ol>\n<li>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 <strong>before the actual content<\/strong>.<\/li>\n<li>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.<\/li>\n<\/ol>\n<h3>Telling browsers to download robots.txt, instead of just opening it right away<\/h3>\n<p>Extending from the <code>robot.txt<\/code> 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 <code>robot.txt<\/code> file. <\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/ Specify that our robots.txt is text document.\r\nheader('Content-type: plain\/text');\r\n\/\/ Tell browsers to open up the download dialog box to download\r\n\/\/ our robots.txt. In addition, the browser should default the \r\n\/\/ file name to robots.txt\r\nheader('Content-Disposition: attachment; filename=&quot;robots.txt&quot;');\r\n\r\n\/\/ Write the contents of robots.txt out to the browser\r\nreadfile(dirname(dirname(__FILE__)) . '\/robots.txt');\r\n?&gt;\r\n<\/pre>\n<p>The script consists of three main lines of codes. We first specify some meta data for our browser via the <a href=\"http:\/\/php.net\/manual\/en\/function.header.php\" title=\"PHP.net reference for the header function\" target=\"_blank\"><code>header<\/code><\/a> function:<\/p>\n<ul>\n<li>The type of content to expect, via <code>Content-Type<\/code>. We tell browsers that robots.txt is text data.<\/li>\n<li>The way in which the content is to be presented to the user, via <code>Content-Disposition<\/code>. 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 <code>robots.txt<\/code>.<\/li>\n<\/ul>\n<p>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 <a href=\"http:\/\/php.net\/manual\/en\/function.dirname.php\" title=\"PHP reference for dirname function\" target=\"_blank\">dirname<\/a> function and the <a href=\"http:\/\/php.net\/manual\/en\/language.constants.predefined.php\" title=\"PHP reference for magic constants\" target=\"_blank\"><code>__FILE__<\/code><\/a> magic constant. It then reads the content of the file via the <code><a href=\"http:\/\/php.net\/manual\/en\/function.file-get-contents.php\" title=\"PHP reference for file_get_contents function\" target=\"_blank\">file_get_contents<\/a><\/code> function and writes the content of the file via the <a href=\"http:\/\/php.net\/manual\/en\/function.print.php\" title=\"PHP reference for print function\" target=\"_blank\"><code>print<\/code><\/a> function.<\/p>\n<h3>Related posts<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" title=\"Downloading a file via HTTP post and HTTP get in C#\" target=\"_blank\">Downloading a file via HTTP post and HTTP get in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" title=\"Sending a file and some form data via HTTP post in C#\" target=\"_blank\">Sending a file and some form data via HTTP post in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/the-http-response-and-how-it-relates-to-system-net-httplistener\/\" title=\"The HTTP response and how it relates to System.Net.HttpListener\" target=\"_blank\">The HTTP response and how it relates to System.Net.HttpListener<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/code-segments-for-rendering-html-4-0-pages\/\" title=\"Code segments for rendering html 4.0 pages\" target=\"_blank\">Code segments for rendering html 4.0 pages<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/generate-php-codes-with-php\/\" title=\"Generate PHP codes with PHP\" target=\"_blank\">Generate PHP codes with PHP<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-3I\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3I&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3I&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F230&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>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 <a href=\"http:\/\/www.techcoil.com\/blog\/handy-tools\/code-generator-for-list-of-countries\/\" title=\"Code generator for list of countries\" target=\"_blank\">tool for generating customized codes for countries in the world<\/a>, which I had created earlier, will tell browsers to prompt a download dialog box after users had submitted the form.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/firefox-open-or-save-file-dialogbox.jpg\" alt=\"Image of firefox dialog box after submitting request to generate custom codes from list of countries in the world.\"\/><\/p>\n<p>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 <a href=\"http:\/\/www.techcoil.com\/robots.txt\" title=\"Link to robots.txt\" target=\"_blank\">http:\/\/www.techcoil.com\/robots.txt<\/a>.<\/p>\n<p>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.<\/p>\n","protected":false},"author":1,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[145,146,50,144,142,23,13,143],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/PHP-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-3I","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/230"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=230"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}