Note: I have tried to implement the solutions provided in : How to fix "Headers already sent" error in PHP and it didnot help so I have posted this question. I have gone through various posts on similar problem but none of the solutions worked. Following is my code:
<form role="form" method="POST">
<input type="submit" name="dff" id="dff" class="btn btn-default" value="Download Franchisee Form"></input>
</form>
<?php
if(isset($_POST['dff']))
{
$yourfile="MyFile.pdf";
$fp = @fopen($yourfile, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourname.file"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($yourfile));
}
else
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourname.file"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($yourfile));
}
fpassthru($fp);
fclose($fp);
}
?>
I get following errors on clicking this button:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 133
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 134
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 135
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 136
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 137
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\GoogleKids\Franchisee.php:116) in C:\xampp\htdocs\GoogleKids\Franchisee.php on line 138
and after that, I get unreadable symbols on the web page something like below:
%PDF-1.5 %���� 1 0 obj <> endobj 2 0 obj <> endobj 3 0 obj <>/ExtGState<>/Font<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 595.32 841.92] /Contents 7 0 R/Group<>/Tabs/S>> endobj 4 0 obj <> stream ����JFIF``��ZExifMM*JQQ�Q�������C !(!0*21/*.-4;K@48G9-.BYBGNPTUT3?]c\RbKSTQ��C''Q6.6QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ��d9"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz����
Note: Even I used ob_start() and ob_flush() at the beginning and end of the script respectively, still i got the same error.
Note: I have not used any other PHP script on complete page. This is the only script. However, I do have another form tag that links to another webpage on performing action and it has its own separate submit button with a unique name and id. Please help.