Error fetching: Can not modify header information [duplicate]

2

When you click on a link, you would like to download a file. The file path comes from the database. The path to the download of the file is correct, something like "catalog / catalog_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 that it gives me error in all the 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
    
asked by anonymous 21.02.2014 / 12:57

2 answers

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>
    
21.02.2014 / 13:13
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 over a connection. If other data is transferred before then it is no longer possible to transfer any header, preventing the header() function from working.

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

    
21.02.2014 / 13:13