Page 1 of 1
How to provide a download functionality
Posted: Thu Jun 06, 2002 4:32 pm
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
Posted: Thu Jun 06, 2002 6:50 pm
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.
Posted: Fri Jun 07, 2002 11:23 am
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
Posted: Fri Jun 07, 2002 11:40 am
by qads
hmm, java script? try useing it that might help, i have no idea on how to do this with php.
Posted: Fri Jun 07, 2002 2:53 pm
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))
{
$field=$rї"field"];
echo $field;
}
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
