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
How to provide a download functionality
Moderator: General Moderators
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
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
I think this is what you're after 
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

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();
?>The ob_start() and ob_end_flush() let you send the headers after the query has happened.
Hope that helps