How to allow users to download files in PHP
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 – this will tell the browser which type of file do you wish to send to user’s browser.
2- Specify the name of the file for user and show an open/save dialog box.
3- Read the original file from your server and render it to users browser.
Here is the php code:
//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');
Yes, thats all. Really.
We will see in a moment how this works. 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.
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 here.In our case it is text/plain as we are going to output a text file.
Next, we provided the value “attachment” for “Content-Disposition” forces an open/save dialog box on the browser. Parameter “filename” tells that the resulting file has to be saved by this name. For more information on Content-Disposition see rfc2183. User will be prompted to save the file by name sample.txt.
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.
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:
///index.html <a href="download.php">Click to download</a>
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.
Thats all.
Sources: PHP Manual http://in2.php.net/manual/en/function.header.php
Would like to read your comments on this article and you are welcome in case of any query.
Related Posts
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


How to download files with PHP…
You do not need scores of lines in your code to achieve that.3 steps and you are done.
We will look in detail what it takes to download a file….
STUMBLED!
Wow, seems so simple, thanks for posting.
Hi Vijay,
As per your comment -["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"]
It will never give an error if you add on top of the code.
With thanks
AMIT BAJAJ
Why you have not posted the whole code. these 3 lines alone cant download a file from database. Without complete code your code is of no help to anyone.
@sharad:
First of all please note that this code is for downloading files from the filesystem not from database.
That is the complete code.Save those 3 lines as download.php.
Then access it from a hyperlink from the other file(index.html in above example)
[...] How to download files with PHP | vijayjoshi.org handy code snippet to enable file downloading with php (tags: php download webdesign file content-type) [...]
I have used similar .php file for prompting visitors to download, but due to some unknown reasons it works on firefox but not on IE
Thanks so much.
Thanks! Keep ‘em coming!
[...] Read more: How to download files with PHP | vijayjoshi.org [...]
Elo, I have a database which stores the path of my file (Different file formats – pdf, word, image, openoffice, etc).. Can you please help me to write the code for downloading the file? Thank you