简体   繁体   中英

Error in setting Header in PHP

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

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

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

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.

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();

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM