views:

65

answers:

2

I have a PHP file that generates xls files using the module found at http://pear.php.net/package/Spreadsheet_Excel_Writer/

I can create the sample document just fine and when I open it, it looks fine.

My next step it to turn it into a downloadable link. To do that, I did this:

    $mimeType = "application/vnd.ms-excel";
    $file_name = "test.xls";
    $file_path = "/tmp/".$file_name;
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Type: application/' . $mimeType);
    header('Content-Length: '.$size);
    header("Content-Disposition: attachment;filename=$file_name ");
    header("Content-Transfer-Encoding: binary ");

    // open the file in binary read-only mode
    // display the error messages if the file can´t be opened
    $file = & fopen($file_path, 'rb');

    if ($file) {
        // stream the file and exit the script when complete
        fpassthru($file);
        exit;

    } else {
        echo $err;
    }

When I download the file however, it contains a lot of garbage data both in Excel and OpenOffice. The diff says that then binary file in the /tmp folder and the downloaded file are different from each other. I'm guessing that it has something to do with the headers or with fpassthru but I haven't had much luck with debugging the issue.

Any ideas on what the problem is?

A: 

I don't know the answer to your question, however, others have reported issues with fpassthru (some version specific). rather than fighting the issue, it might be best to use feof/fread instead. this page : http://php.net/manual/en/function.fpassthru.php : seems to have some good wrappers for sending a file back.

Don Dickinson
+1  A: 

The multiple Content-Type headers are uncessary. You're essentially saying that the file is a muffin and a pizza and a ford taurus all at the same time. All you need is the application/octet-stream version, unless you want to serve up the exact mime type.

As well, is there any reason you're trying to turn the file handle returned by fopen() into a reference?

Try something simpler:

<?php

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=$file_name");

readfile("/tmp/test.xls");

exit();
?>

and see if that does any better.

Marc B
I tried replacing my code with the given and it's still having the same issue. Also, I just copied the section of code that passes it as a reference so I admit that I wasn't sure myself on why it was being done that way. I tried it with and without the ampersand and it didn't make a difference in the output.
Matthew
Have you looked inside the file with a text editor to see what the garbage is? Could be error messages/warnings from PHP, a stupid browser plugin mungeing things, etc...
Marc B