Error forcing download: Cannot Modify header information

Asked

Viewed 1,217 times

2

By clicking a link, I wanted to download a file. The file path comes from the database. The file download path is correct, something like "catalogo/catalogo_janeiro.pdf".

<?php
$sql = mysql_query("SELECT * FROM tbl_catalogo WHERE id = '1'");
$row = mysql_fetch_array($sql);
$catalogo = $row['link']; // CAMINHO DO FICHEIRO

header("Content-disposition: attachment; filename=$catalogo");
header("Content-type: application/pdf");
readfile("$catalogo");
?> 

The link to the download:

<a href="<?php echo $catalogo;?>" target="_blank"><img src="images/catalogo.jpg" /></a>

The problem is it gives me error in all header lines();

Warning: Cannot modify header information -
headers already sent by
(output started at /home/inforcyb/public_html/opaco/header.php:15)
in /home/inforcyb/public_html/opaco/index.php on line 181
  • What’s the matter?

  • Warning: Cannot Modify header information - headers already sent by (output Started at /home/inforcyb/public_html/opaque/header.php:15) in /home/inforcyb/public_html/opaque/index.php on line 181 Gives error on all header lines();

  • This type of error happens when you have a texte output before a header(), sometimes this can be a problem in the enconding of the file that sends a character (GOOD) before the header

  • I have already added the answer with resolution to my problem, thank you!

2 answers

1

Warning: Cannot modify header information - headers already sent by
(output started at /home/inforcyb/public_html/opaco/header.php:15) in
/home/inforcyb/public_html/opaco/index.php on line 181

HTTP headers are the first data to be transferred on a connection. If other data is transferred before then it is no longer possible to transfer any header, preventing the function header() work.

Make sure that headers are sent before any other content: the error itself shows that at line 15 of header.php data has already been sent. Change its structure so that the headers are first sent, then the contents.

  • I have already added the answer with resolution to my problem, thank you!

  • 2

    I leave my answer, each one solves in a different way, but correct. I just have a problem against scripts get_file.php?file=...: the vast majority of them have a transverse path. I already used this flaw against a site, the other day I went to the website developer’s office personally to talk about the error. Tidy up your code.

1


Create the pdf_server.php file with:

<?php
header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
} 
fclose($fp); 

?>

On the page where you have the download link:

<?php
$sql = mysql_query("SELECT * FROM tbl_catalogo WHERE id = '1'");
$row = mysql_fetch_array($sql);
$catalogo = $row['link'];
$link = "pdf_server.php?file=".$catalogo;
?> 

<a href="<?php echo $link;?>" target="_blank"><img src="images/catalogo.jpg" /></a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.