Another PHP question

sigmend

[H]ard|Gawd
Joined
Aug 6, 2003
Messages
1,303
I have a MySQL database with information in it that I want exported to a CSV file.
This will work in Safari and FireFox, but not IE. Hep?

PHP:
<?php
session_start();
header("Cache-control: private"); // IE6 fix
include('../MyResources/include.php');
$WHD_DB = WHD_DB_CONNECT();
//Login checking that is not relevant to this Q
$csv_output = "Asset_Number,Checked_in_by,Date,Time,SID,First_Name,Last_Name,Status,Assessment(Commas removed),Ticket_number,Charger,ByIP,Edited";
$csv_output .= "\n";
$Query = query("select * from checkin",$WHD_DB);
while($row = mysql_fetch_array($Query)) {
    //We need to get rid of commas because they screw up the CSV file.
    //Assessment is the only place where commas could appear
    $Assessment = str_replace(","," ",$row['Assessment']);
    $csv_output .= "$row[Asset_Number],$row[Checked_in_by],$row[Date],$row[Time],$row[SID],$row[First_Name],$row[Last_Name],$row[Status],'$Assessment',$row[Ticket_number],$row[Charger],$row[ByIP],$row[Edited]";
    $csv_output .= "\n";
    }
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=" . date("Y-m-d") . "_Checkin_Dump.csv");
print $csv_output;
exit;
?>

with query() being
PHP:
function query($query,$db_cnx) {
    $result = mysql_query ($query, $db_cnx);
    if(!$result) {
        //Email is set in the config file. If the admin does not want error reporting on, they set it to 1
        if($Email == 0) {
		$Message = "Something failed inside of the checkin program.\nInformation is as follows\nFailed with: '" . $query . "'\n" . mysql_error() . "\nWhen running " . $_SERVER['PHP_SELF'] . "\n From user: " . $_SESSION['name'];
        	mail("[email protected]", "Query failure", $Message); 
        	echo "Email report sent to dev<br />";
	} else {
	}
   	echo "Error.. <br />MySQL said : <br /><textarea readonly='readonly' cols='120' rows='2'>" . mysql_error() . "</textarea>";
       	echo "<br />With query: <br /><textarea readonly='readonly' cols='120' rows='2'>" . $query . "</textarea>";
        
	die();
    } else {
    }
    return $result;
    }
 
Instead of doing application/vnd.ms-excel, just do application/download and it should just initiate a download.
 
That's nice that a excel open works with two browsers that are not M$, but doesn't work with the M$ browser.... :rolleyes:
 
That didn't fix it.
http://www.iupload.net/022005/wtferror.png

I am going to be picking apart the proper headers now that I have ethereal installed, but if anyone has anything to say, go ahead

edit: Because I don't want to bump this thread, I am just going to edit my post
This works
PHP:
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-control: private"); // IE6 fix
   header("Content-Description: File Transfer");
   header("Content-Type: application/octet-stream");
   header("Content-Disposition: attachment; filename=". date("Y-m-d") . "_Checkin_dump.csv" );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ". strlen($csv_output));   
   print $csv_output;
   exit;
 
i noticed when i was trying to force a download for an on the fly generated zip file that if i typed the url directly and hit enter or f5 to refresh, it didn't work, i had to link to it from another page. not sure if this is the real issue
 
Back
Top