<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vijayjoshi.org &#187; download</title>
	<atom:link href="http://www.vijayjoshi.org/tag/download/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vijayjoshi.org</link>
	<description>php &#124; javascript &#124; ajax &#124; and all things web</description>
	<lastBuildDate>Sun, 20 Nov 2011 15:24:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to allow users to download files in PHP</title>
		<link>http://www.vijayjoshi.org/2008/11/25/how-to-allow-users-to-download-files-in-php/</link>
		<comments>http://www.vijayjoshi.org/2008/11/25/how-to-allow-users-to-download-files-in-php/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 11:15:26 +0000</pubDate>
		<dc:creator>Vijay Joshi</dc:creator>
				<category><![CDATA[FAQ]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[download]]></category>

		<guid isPermaLink="false">http://www.vijayjoshi.org/?p=132</guid>
		<description><![CDATA[
			
				
			
		
So, you want to allow your users to download files from your server.
Downloading files in php is a simple process.You do not need scores of lines in your code to achieve that.3 steps and you are done.
A working example for the same can be seen here.
Steps:
1- Specify content type &#8211; this will tell the browser which type of file do ...]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F11%2F25%2Fhow-to-allow-users-to-download-files-in-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F11%2F25%2Fhow-to-allow-users-to-download-files-in-php%2F&amp;source=v08i&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>So, you want to allow your users to download files from your server.<br />
Downloading files in php is a simple process.You do not need scores of lines in your code to achieve that.3 steps and you are done.<br />
A working example for the same can be <a title="Download file" href="http://www.vijayjoshi.org/examples/download/index.html" target="_blank">seen here</a>.</p>
<p><strong>Steps:</strong><br />
1- Specify content type &#8211; this will tell the browser which type of file do you wish to send to user&#8217;s browser.<br />
2- Specify the name of the file for user and show an open/save dialog box.<br />
3- Read the original file from your server and render it to users browser.</p>
<p>Here is the php code:</p>
<pre class="brush:php">//download.php
//content type
header('Content-type: text/plain');
//open/save dialog box
header('Content-Disposition: attachment; filename="sample.txt"');
//read from server and write to buffer
readfile('test.txt');</pre>
<p>Yes, thats all. Really.<br />
We will see in a moment how this works. <span id="more-132"></span>First remember that there must not be any line besides these lines in the file. Also note that outputting anything to the browser,like blank spaces or any html before a call to header function as it will throw an error.</p>
<p>First we specified a content type using the header function.header is used to send http headers to the browser.Content type will be set depending on the file type to be downloaded. It can be text/html for a html file,text/xml for xml file or application/zip for zip files. You can see a list of content types <a title="List of Content Type" href="http://www.vijayjoshi.org/blog/resources/content_types.txt" target="_blank">here</a>.In our case it is text/plain as we are going to output a text file.</p>
<p>Next, we provided the value &#8220;attachment&#8221; for &#8220;Content-Disposition&#8221; forces an open/save dialog box on the browser. Parameter &#8220;filename&#8221; tells that the resulting file has to be saved by this name. For more information on Content-Disposition see <a title="RFC2183" href="http://www.ietf.org/rfc/rfc2183.txt" target="_blank">rfc2183</a>. User will be prompted to save the file by name sample.txt.</p>
<p>Finally, the readfile function. readfile reads a file from server and writes it to the outputbuffer, which in our case is users browser.The parameter test.txt here is the name of file on server.</p>
<p>Opening this file in your web browser will start download.Alternatively, you can provide a download link from other file also.For this, create a html file like below:</p>
<pre class="brush:html">///index.html

<a href="download.php">Click to download</a></pre>
<p>Save this with whatever name you prefer. Now open it in your browser and Click the link. You will be prompted with the familiar the open/save dialog box.</p>
<p>Thats all.<br />
Sources: PHP Manual <a href="http://in2.php.net/manual/en/function.header.php">http://in2.php.net/manual/en/function.header.php</a></p>
<p>Would like to read your comments on this article and you are welcome in case of any query.</p>
<div class="shr-publisher-132"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:right;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.vijayjoshi.org%2F2008%2F11%2F25%2Fhow-to-allow-users-to-download-files-in-php%2F' data-shr_title='How+to+allow+users+to+download+files+in+PHP'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom -->]]></content:encoded>
			<wfw:commentRss>http://www.vijayjoshi.org/2008/11/25/how-to-allow-users-to-download-files-in-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

