How to provide a download functionality

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gbadgi
Forum Newbie
Posts: 2
Joined: Thu Jun 06, 2002 4:32 pm

How to provide a download functionality

Post by gbadgi »

Hello

I am trying to code a download file function using PHP. I basically query a databse get some data would like to present the user with a save as dialog.

Can anybody help me with this.

Thanks

Girish
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

i have no clue what you're on about....expline a bit more then that.

normaly if u want people to download a file u'd zip it up and give a link or ask them to right click > save file as.

but i don't know what you mean.
gbadgi
Forum Newbie
Posts: 2
Joined: Thu Jun 06, 2002 4:32 pm

Post by gbadgi »

Sorry for not making it clear. I would like to allow a user to query the database and save the result set in a comma delimited file on the user's local computer.

One of the options would be to a make a file and provide a link to the file and ask the user to right click and save-as.

Is there a way to pop-up the save as dialog automatically from a PHP script? I have seen such functionality on some sites which allow for download of data. For ex. Etrade allos to save your transactions as a CSV.

Thanks in advance

Girish
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

hmm, java script? try useing it that might help, i have no idea on how to do this with php.
Oxy
Forum Newbie
Posts: 17
Joined: Fri Apr 19, 2002 5:47 am
Location: U.K
Contact:

Post by Oxy »

I think this is what you're after :)

Code: Select all

<?php
ob_start();

//connect
$db = mysql_connect("localhost","user","password");
		  mysql_select_db("database");

$query = "SELECT * FROM table";

$result=mysql_query($query);

while($r=mysql_fetch_array($result))

&#123;

$field=$r&#1111;"field"];

echo $field;
&#125;

header("Cache-control: private");
header("Content-Type: application/txt");
header("Content-Disposition: attachment; filename=$field.txt");

ob_end_flush();
?>
That brings up a save as dialog in the browser, and will let the user download the contents of 'field' in your database, and save as text file.

The ob_start() and ob_end_flush() let you send the headers after the query has happened.

Hope that helps

:D
Post Reply