i want to export from mySQL to a comma-separated file and i want to let the user download the file and save it somewhere or open it. to create the file, i do this:
$export_filename = TMP_DIR . uniqid('blah');
if (!($export_file = fopen($export_filename, "a"))) {
die("COULD NOT OPEN TEMPORARY FILE FOR EXPORT");
}
// my data routines are here which query, return a table
// and loop through the fetched arrays, writing one line
// at a time (rather than doing dump buffering)
// each line is ended by "\n"
fclose($export_file);
jta_download($export_filename, "POE_TRAVEL_DATA.CSV");
//and HERE is my download function
//this is what i'm worried about....it seems to have problems
// sometimes i'll get a message (see below).
function jta_download($file,$filename){
$size=filesize(urldecode($file));
$attachment=(
strpos($HTTP_USER_AGENT,"Mozilla/4")===false // Not Moz4
|| // or
strpos($HTTP_USER_AGENT,"MSIE")!==false) // is IE
? ' attachment;'
: '';
$content_disposition = "Content-Disposition:" . $attachment . " filename=" . urldecode($filename);
header($content_disposition);
header("Content-Type: text/x-csv");
header("Content-Length: $size");
readfile(urldecode($file));
}
i sometimes get an error that internet explorer can't find the file in the temporary internet files folder and do i want to create a new one. this header and mime type stuff is all very new to me. any advice would be much appreciated.