user2432939
user2432939

Reputation: 1

Transferring Excel via PHP header()

An Excel file is stored in a database (hexvalues of former binary data). I need to read it and make it available for download.

Here's the code...

$out = hex2bin($out); // the stuff from the database


header("Content-Type:   application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='$filename'");  
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
die($out);

Actually I receive stuff... and it looks like the original file with a slight difference. When using a hex-editor, it shows

    ÐÏࡱá

instead of

ÐÏࡱá

(looks like an additional tab). So Excel isn't able to show the sheet but the raw data.

That's not from the data in $out; the conversion bin2hex (during import in database) and hex2bin (export from database) works without problems. If I write $out to a file via fopen / fwrite, it's exactly the original Excel file (which I can open normally).

How can I accomplish this?

Upvotes: 0

Views: 240

Answers (1)

rixo
rixo

Reputation: 25021

Ensure you don't have this extra tab character before the opening <?php tag or after the closing one ?>. Actually, it is good practice to omit the closing tag exactly for this reason.

Upvotes: 1

Related Questions