Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySql to CSV Export Problem 1

Status
Not open for further replies.

jsnull

IS-IT--Management
Feb 19, 2002
99
US
Hi I have this code, along with a MYSQL statement:

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=helpbuildhope.csv");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");

That I am using to pull data out of a MySQL dB to a local CSV file. It works wonderfully in Firefox! However, in IE 6.+ it fails with the following error message: "Internet Explorer cannot download sccget.php from Internet Explorer was not able to open this Internet site. The requested site is ether unavailable or was not found. Please try again later." Along with the error dialogue box is also the typical IE download box that is not downloading anything. You can see a screen shot at:
The easy solution is to just use Firefox, but I can't guarantee that the folks using this web page will always do so. Suggestions, thoughts?





Jim Null
[afro]
 
please recast your headers like this
Code:
header("Content-type: application/octet-stream");
clearstatcache();
header("Content-Length: " . filesize("helpbuildhope.csv");
header('Content-disposition: attachment; filename="helpbuildhope.csv"');
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
i.e. you should provide a content length. the value of this is cached so if there is any chance of this changing you should flush the cache before testing the file size.
but the real trick here is that the filename in the content disposition should be enquoted.
 
With this code in place I am receiving the following error:

Parse error: parse error, unexpected ';' in filenameinfo on line 3


Jim Null
[afro]
 
sorry. in the content length header there is a missing close brackets just before the semicolon
 
Wowser ... get the same results in IE as before ... but also FireFox now downloads the php web file that has this info in it:

Code:
<br />
<b>Warning</b>:  filesize(): Stat failed for helpbuildhope.csv (errno=2 - No such file or directory) in <b>folderinfo</b> on line <b>71</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at folderinfo:71) in <b>folderinfo</b> on line <b>71</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at folderinfo:71) in <b>folderinfo</b> on line <b>72</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at folderinfo:71) in <b>folderinfo</b> on line <b>73</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at folderinfo:71) in <b>folderinfo</b> on line <b>74</b><br />
<br />


Jim Null
[afro]
 
then the csv file is not in the directory of the script. in the filesize call you need to tell php where to find the file you are wanting to download.

if it is a string that you are echoing then replace filesize() with strlen($string)
 
I am actually attempting to create the file from a MySql query.

The sequence is:

1. headers
2. MySql
3. Echo the MySql results
4. File then downloads

Firefox handles this with great ease and pleasure and IE doesn't.

Jim Null
[afro]
 
ok. you still need to enquote the filename and you should provide a content length

Code:
$results = "Select * from addresses";
$dl = "";
while ($row=mysql_fetch_assoc($results)):
 $dl .= implode (",", $row) . "\r\n";
endwhile;
header("Content-type: application/octet-stream");
clearstatcache();
header("Content-Length: " . strlen($dl));
header('Content-disposition: attachment; filename="helpbuildhope.csv"');
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
echo $dl;
 
That's working perfectly in Firefox, too... but still not IE... IE acts like it is trying to download the web page, such as getdata.php, rather than the CSV file. Here's my full code at this point ...

Code:
$query = mysql_query("SELECT * FROM $tablename WHERE 'regdate' like $getdate");

$dl = "";
while ($row=mysql_fetch_assoc($query)):
 $dl .= implode (",", $row) . "\r\n";
endwhile;

header("Content-type: application/octet-stream");
clearstatcache();
header("Content-Length: " . strlen($dl));
header('Content-disposition: attachment; filename="helpbuildhope.csv"');
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
echo $dl;

mysql_close($global_dbh);

I read this interesting comment "Note: There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later." at Do you think this may be the issue for IE even though I am using 6.+ that has all the current updates?


Jim Null
[afro]
 
Fixed ... here's the final piece that did it. I changed the Pragma header to:

header("Pragma: public");

and it works perfectly in IE and Firefox ... thanx for all of your help and getting me on the right track!

Jim Null
[afro]
 
Code:
$query = mysql_query("SELECT * FROM $tablename WHERE 'regdate' like $getdate");
i'm surprised this works. it looks like you are enquoting a column name rather than a criteria

i have several sites running downloads like this. none cause a problem on either IE or FF or any other browser that i've come across.

i should add that the code i've posted above won't generate a proper csv file as it does not enquote the fields. but that won't have any effect on the download.

i'm happy to test the dl from here if you post a link.

you can remove clearstatcache() as you are not making a file call.

two other thoughts. try forcing the content transfer to binary.
Code:
header("Content-Transfer-Encoding: binary\n");
and i've noticed in all my scripts that the D of Content-Disposition is always capitalised. i have no idea whether the browsers are case sensitive but just in case ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top