-1

below is some part of code in my download gateway

if (!isset($_GET['f']) || empty($_GET['f'])) {die("<h1>URL Malfunction</h1><br/><p><i>Please Try Later</i>");}
if (strpos($_GET['f'], "\0") !== FALSE){ die("<h1>URL Malfunction</h1><br/><p><i>Please Try Later</i>");}
                            #Check URL, find resource Path

$fileName = basename($_GET['f']);
$file_path=(string)makeDownloadFilePath($fileName,"dir");

if(!is_file($file_path)){die("<h1>404 Not found</h1><br/><p><i>The resource you requested is not available</i>");}

$fileSize = filesize($file_path); 



header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");                    #Build Response#
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"$fileName\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fileSize);


$file = @fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {                 #File Transfer#
   print(fread($file, 1024*8));
   flush();
   if (connection_status()!=0) {
    @fclose($file);
    die();
         }
    }
 @fclose($file);

//The File is Downloaded . Closing Connections

I am using GET method to receive the filename. The filename and its path will e genrated from gateway. Now the problem is When i click on download in a page, instead of showing a Download dialog, the browser just renders the file content as text on screen. For eg, i am downloading foo.mp3. the binary contents are displayed as weird text on screen. Its echoing a warning like: We cannot change the Headers. headers already sent to ...

Can any one tell , where i had made the mistake?

Thanks

3
  • That's not the error message. Just copy and paste the real error message into the Google, and you'll find out.
    – mario
    Oct 27, 2011 at 10:27
  • possible duplicate of Headers already sent
    – mario
    Oct 27, 2011 at 10:37
  • Error message? I can see only that warning in the browser. then it starts rendering the file as text
    – Kris
    Oct 27, 2011 at 10:40

5 Answers 5

1

We cannot change the Headers. headers already sent to..

This error comes when you print any thing before php your header command.

6
  • i have used die() on if conditions. But this will happen only on if true right?.
    – Kris
    Oct 27, 2011 at 10:45
  • you die's are ok, but you print something somewhere else, thats for sure. Are you maybe including this file?
    – peko
    Oct 27, 2011 at 10:50
  • you mean include()?. I have 4 include directives in this file.
    – Kris
    Oct 27, 2011 at 11:01
  • 1
    any includes before header? and as mentioned check for unintended whitespaces before <?php
    – peko
    Oct 27, 2011 at 11:04
  • i tried with headers_sent(), it says Headers was sent in line number 0
    – Kris
    Oct 27, 2011 at 11:20
1

The most common cause of this error by a long, long way is that you have some leading white-space before the opening <?php tag in your file (or one of it's includes).

The < should be the first character in the file, anything before it is written to the output buffer directly and will probably result in the headers being sent. When forcing file download in this manner, it will also result in corrupted files.

2
  • I dont have any spaces before '<'
    – Kris
    Oct 27, 2011 at 11:35
  • Have you checked your included file(s) as well?
    – DaveRandom
    Oct 27, 2011 at 11:49
0

Use readfile instead of fopen as follow and use ob_clean() , ob_flush() :

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$Name.'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: '.filesize($musicPath));
    ob_clean();
    flush();
    readfile($musicPath);
    ob_flush();
1
  • No change. its the same output.
    – Kris
    Oct 27, 2011 at 10:38
0

Are you using the output buffer?

try adding ob_start(); before you send out the header information, this may solve your issue.

You can find out more information about it here

0

Thanks all for the help. The problem was i was using a

error_reporting(E_ALL);
ini_set('display_errors', true);
flush();

for debugging in one of my includes.

I just removed it.Now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.