-2

I've got a site with a table and want to add a button in front of each tr. Clicking on the button should pop up the download dialog (save file) and after pushing "save", download the file.

I see this so often, and still don't know how to do that. I have set up the button via a form:

<form method="get">
<input type="submit" value="<?php echo $paket; ?>" name="download" />
</form>

where $paketcontains the filename without the .zip ending.

In the same php file, just under two require_once statements, I did this:

if ( isset( $_GET['download'] ) ) {
$name = $_GET['download'];
header( 'Content-Disposition: attachment; filename = '.$name.'.zip' );
header( 'Content-type: application/zip' );
}

If I push the button, I get these errors:

Warning: Cannot modify header information - headers already sent by (output started at /is/lib/require.req:154) 

I do know that the header information needs to sit on the very top, but how am I supposed to use functions I include with require_once, if the require_once comes a few lines after?

Is there a nice (and foolproof) tutorial that I just can't find?

Adi
  • 5,089
  • 6
  • 33
  • 47
Nareille
  • 811
  • 2
  • 11
  • 30
  • 1
    Your output begins before your `header(...)` lines. There's either whitespace or some other kind of output. You can't do that. – Matt Aug 09 '12 at 14:46
  • 3
    You also might want to google "Warning: Cannot modify header information". It turns up a surprising number of relevant matches, oddly enough. – Matt Aug 09 '12 at 14:47
  • 1
    possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – Marc B Aug 09 '12 at 14:48
  • have you tried output buffering ?? – Umair Khan Aug 09 '12 at 14:49

4 Answers4

1

Use an output buffer if you need to send content before the headers http://php.net/manual/pt_BR/function.ob-start.php . Anyway your donwload file should be on a different location or put the download handling part at the very top off the file and exit after it

FabioCosta
  • 3,069
  • 6
  • 28
  • 50
0

Do not use echo, print() or something similar before header() or start_session(). HTTP-sepcific information has to be before common-output.

Just look at the php-documentation

"Remember that header() must be called before any actual output is sent [...]"
NaN
  • 3,501
  • 8
  • 44
  • 77
0

The header must always been send before any other output took place. That means it must be the first thing send in the current file (which contains the header information) but it also has to be the first thing send for all other files, that include this file.

Won't work

<?php
    // application logic here
    print "X";
    require 'header.php';
    // application logic here

Will ork

<?php
    // application logic here
    require 'header.php';
    // application logic here

header.php

<?php
    // application logic here
    header( 'Content-type: application/zip' );
    // application logic here
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
0

It turned out there was a whitespace before the first <?php tag, that caused the error.

Nareille
  • 811
  • 2
  • 11
  • 30