Jump to content

Writing an excel file or redirecting the user, but not both. Help!


lamajlooc

Recommended Posts

Hi,
So I'm writing a script that reads various data from the DB, writes and Excel file and (hopefully) redirects the user to a confirmation page after that's done.  The problem is that it either writes the files fine and doesn't redirect or redirects fine but won't write the file.  I'm using header and I'm getting a conflict.

Here's the code:
if ($header!="" && $data!=""){
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$title.xls");
header("Pragma: no-cache");
header("Expires: 0");
//header("Location: exportresult.php"); This is commented out so the file writes.
print "$header"."$data";
} else {
header("Location: exportproblem.php");
}

I was thinking about using fwrite to write the file but that seems very cumbersome.  Any other ideas?
It looks like you're forcing a download of a file. If the file has already been written to disk, you can use readfile() to read and output to buffer (for download).

I don't think you can do both download and redirect, it's one or the other.

Thanks for posting this question.  I learned something as a result of it too.

I've been wondering how to create a file on the fly to be downloaded without having to save the file to the server first.  I thought there had to be a way.
  • 1 year later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.