0

A "headers already sent" warning arises .I close $zip->close(); but still show warning

$zip = new ZipArchive;
$download = 'new_zip.zip';
$zip->open($download, ZipArchive::CREATE);
foreach (glob("zipfile/*") as $file) { 
    $zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
takeone
  • 37
  • 1
  • 5
  • what happens if you insert `if(headers_sent($from, $line)) die ('Headers were already sent at : '.$from.' on line : '.$line);` below `$zip->close();` ? – spinsch Aug 10 '14 at 09:45
  • 2
    you have to provide full code of your page. possibly, there's some html before – vladkras Aug 10 '14 at 09:46
  • try adding at the very beginning of the code – Anam Ahmed Aug 10 '14 at 09:46
  • Check the output of your code. If something went wrong an output will displayed. The `header()` only works if no output before is displayed. Any (non-visible) output like HTML-tags are output too. – Sebastian Brosch Aug 10 '14 at 09:49
  • That error arise because you have already sent some output on the page, have a look at [How to fix Headers Already Sent](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Erlesand Aug 10 '14 at 10:26

1 Answers1

3

At the start of your script, use:

<?php ob_start(); ?>

Then at the end of your script, add:

<?php ob_flush(); ?>
James
  • 576
  • 2
  • 8
  • 20
  • While this will probably make the warning go away, it does not solve the underlying problem that some code is producing output that it shouldn't. – rethab Aug 10 '14 at 11:32