Clearing headers so I can send a file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am trying to save a .csv file to the local machine. One of the
program's functions, echoes status text to the screen. When I set the
header, and echo the string I want saved as a .csv file, I get the
following message:
Warning: Cannot modify header information - headers already sent by
(output started at /home/tools/public_html/exportJobToCSV.php:132) in /
home/tools/public_html/exportJobToCSV.php on line 60

I tried adding ob_clean, prior to echoing the .csv string, but I got
the same error message. For this piece of code,  $csvStructure is the
CSV formatted string.
echo "We are now processing unit # $unitID";

//clean output buffer and ready file output
// send headers to cause a save file prompt on the local machine
header("Content-Type: text/csv");
header("Content-Disposition: Attachment; filename=csvFile.csv");
header("Pragma: no-cache");
echo $csvStructure;

Re: Clearing headers so I can send a file


on 02/17/2010 09:56 PM JustWondering said the following:
Quoted text here. Click to load it

There is no way to clear the headers already sent to the browser.

What is happening to you is that you are outputing something in your
script before PHP executes those header lines. Take a look at the script
/home/tools/public_html/exportJobToCSV.php in line 132. That is where
your output is starting.

You can use output buffering before executing that line, but I suspect
that what you output in that line may not be intended.


Manuel Lemos

Find and post PHP jobs /

PHP Classes - Free ready to use OOP components written in PHP /

Re: Clearing headers so I can send a file

Quoted text here. Click to load it

Yes, I am outputing something in the routine on line 132, which is
what I mentioned in my original post. That's diagnostic information
that must be echoed. The problem is that the header is already sent,
so I can no longer force a save of the CSV file to local disk. Which
why I am asking if anyone can suggest a different way of forcing a
save to local machine.

Re: Clearing headers so I can send a file


Quoted text here. Click to load it

You can't output a HTML page and a file at the same time. One request,
one response. Write your diagnostic informations to a logfile instead.


Re: Clearing headers so I can send a file

On Thu, 18 Feb 2010 03:46:39 +0100, Michael Fesser wrote:
Quoted text here. Click to load it

Not the same stuff, transparently, but you can certainly output to
stdout (a web page generation) and independently to a Large Number of
named file handles in the same script.

8. After I kidnap the beautiful princess, we will be married
   immediately in a quiet civil ceremony, not a lavish spectacle in
   three weeks' time during which the final phase of my plan will be
   carried out.  --Peter Anspach's Evil Overlord list

Re: Clearing headers so I can send a file

JustWondering says...

Quoted text here. Click to load it

Output the diagnostic, sleep x seconds on the page, then redirect to new
page with no headers for the output ??  Untested idea only.


Re: Clearing headers so I can send a file

Quoted text here. Click to load it

Or, produce the file while outputting diagnostics.  Once the file is
created on the server file system, just provide a link to it.  If you
don't want the file to persist on the server, or be accessible via the
DOCUMENT_ROOT, then write it to /tmp, and provide a link to something
like: deliver.php?file=3Dfilename.csv, where deliver.php will write the
appropriate headers, output the file contents to STDOUT and then
unlink the file.  Pseudo-code follows.

Existing script:

$filname = "file.csv";
$f = fopen("/tmp/$filname"...);

do stuff, output diagnostics


  <a href=3D"deliver.php?file=3D$filename">Download CSV File</a>


// validate $_GET['file'] --you don't want someone to pass "../etc/
passwd" :)

// send CSV headers (whatever those look like)

echo file_get_contents("/tmp/");


That's how I'd handle it, if not just delivering the file via HTTP and
writing my diagnostics to a logfile.  I have to admit, however, there
have definitely been days when my life would have been a lot easier if
HTTP had a multipart delivery method.

Re: Clearing headers so I can send a file

Another way I would  have done it:

echo "We are now processing unit # $unitID. <a href=3D'download.php?
id=3Dsomefileid_encrypted_format_if_possible'>Click here to download</a>
the file";

In download.php do the processing for csv generation and send
appropriate headers for save.


Quoted text here. Click to load it

Site Timeline