vijayjoshi.org - php | javascript | ajax | and all things web

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

Click to download

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

Posted in : FAQ, PHP
Tags: ,

9 Comments to “How to allow users to download files in PHP”

Add Comments (+)

  1. Geoserv says:

    STUMBLED!

    Wow, seems so simple, thanks for posting.

  2. Amit says:

    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

  3. sharad says:

    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.

  4. Vijay says:

    @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)

  5. Aniruddha says:

    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

  6. Luke says:

    Thanks so much.

  7. Danny says:

    Thanks! Keep ‘em coming!

  8. Boochia Ritesh says:

    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

  9. Hakoo says:

    Hey,
    You have given nice code. I am newbie in PHP. Can you tell me how can I write this in MVC framework?

Trackbacks/Pingbacks

  1. Added by a PAL to FAQ PAL
  2. links for 2009-04-05 on studiowhiz.com
  3. How to download files with PHP | vijayjoshi.org

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

55,816 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>