Forum Moderators: coopster

Message Too Old, No Replies

Generating a CSV for Download from MySQL

         

H2O_aa

9:22 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



I need to create a webpage with a download button, which when a user clicks on, the data from the mysql table will be written into a csv file and a Save as dialog window will pop up allowing the user to save or open the csv file. So basically I need the same thing as what the thread starter stated here:
[webmasterworld.com...]

So from that thread, I found this code jatar_k (our beloved admin) created.

I took out some lines since I wasn't sure what they do. So this is what I am using:

download.php

<?
if($submit) {
include "my_db.php";
$content_file .= "first,last,email\r\n";
$sql = 'select * from categories';
$statement = mysql_query($sql);
while($row = mysql_fetch_array($statement)) {
$content_file .= $row['first'] . ',' . $row['last'] . ',' . $row['email'] . "\r\n";
}
$output_file = 'mygreatcsvfile.csv';
@ini_set('zlib.output_compression', 'Off');
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');
//This should work for IE & Opera
header('Content-Type: application/octetstream; name="' . $output_file . '"');
//This should work for the rest
header('Content-Type: application/octet-stream; name="' . $output_file . '"');
header('Content-Disposition: inline; filename="' . $output_file . '"');
echo $content_file;
exit();
}
?>

<form name="form1" method="post" action="download.php">
<input type="submit" name="submit" value="Download">
</form>

When I press the Download button, I get a File Download dialog window. The code works fine if I choose Save. I then can specify where to save the file. But when I choose Open, it wouldn't work. I will get a error message that says "Excel has detected that 'download.php' is a SYLK file, but cannot load it......."

H2O_aa

10:26 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



what does

echo $content_file;

do at the end?

jatar_k

6:56 am on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmmm, when coopster and I (well mostly coop) were pounding through this I can't remember if that was an issue or not.

>> what does echo $content_file; do at the end?

well, once the headers are created properly we need to output the content of the file itself

coopster

2:11 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Content Negotiation was the issue at that time (the thread referenced originally in msg #1 has the solution for that). In this case, Excel seems to think you have a corrupted file or something. I do recall MS saying something about adding an Accept-Ranges header for a fix for something along the lines once, but I'm not so sure this is it.
header('Accept-Ranges: bytes');

I do a lot of searching over the MS support site when I run into these issues. Often times it comes down to their software and we end up throwing tweaks in all over the place trying to figure out what it is. I agree with jk, get the headers correct and you should be good to go. Create a working copy of your script and then start manipulating headers.