Jump to content

Fairly new at this... headers sent, csv file downloads, need to process more.


cwilkinson

Recommended Posts

I have a script which displays a form. When clicking submit, the script runs a query, if results are found, the script creates a CSV file and the browser prompts the user to download the file. 

 

If no results are found, an error level is set, the form is displayed again, the error is echoed.

 

It works fine and it gets the job done.  I'm happy I got this far. :)

 

However.

 

I'm trying to polish the script a so that when the user clicks submit, the file is downloaded, and then the script displays the number of rows that have been download, the file name downloaded and then displays the form again for additional input.

 

I think my question is about outputting information to the browser before or after the user is prompted to download. 

 

Currently, I use one script.  The form is after the php form processing, and this is the bit that saves the file.  I don't think the rest of the script is important...

 

if ($data != "0") 
{
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
}

 

Anything after print "$header\n$data"; does not get executed.

 

Perhaps a better method is to save the file server-side then provide a link to download it. Is that the better method?

 

I did some googling and to be honest, finding the proper way to ask the question is the biggest challenge, for me, in learning php or any type of language.

 

Thanks in advance,

 

-

Chris.

 

I get it now.  The headers are telling the browser to save all output as the CSV file.  Anything after the headers is being shoved into the file and download.  That's what I get for using free scripts.

 

With that said, I've confirmed the method of saving the file locally is preventing me from the final product.  I have to seek a different method.

 

I've considered writing $header and $data to a file on the server then downloading the file as a link, but that gets sloppy in that I don't know how to remove the file after downloading.

 

Seeking advice for the above now. :)

 

Cheers!

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.