简体   繁体   中英

PHP text file auto download giving error headers already sent

I am uploading text file and then converting it after conversion I am forcing it to auto download that converted text file, but it's not getting downloaded, giving error Warning: Cannot modify header information - headers already sent

Force download text file PHP code

if (file_exists($fileCreate)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileCreate));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileCreate));
    ob_clean();
    flush();
    readfile($fileCreate);
    exit;
}
  }

Full PHP code

<?php
error_reporting(E_ALL & ~E_NOTICE);
if(!empty($_FILES['uploaded_file']))
  {
    $path = "upload/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    $count = 0;
    $fileCreate = 'order-'.date('m-d-Y').".txt";
    $myfile = fopen("$fileCreate","w") or die("Unable to open file!");
    fwrite($myfile, "1"."\n");
    if($file = fopen("$path", "r")){
        while(!feof($file)) {
            $line = fgets($file);
            $keywords = preg_split("/[\s,]+/", $line);
            $x = $keywords[2];
            $y = 5;
            $y .= $x;
            $y .= "001";
            $date = $keywords[1];
            $date1 = str_replace('"','',$date);
            $newDate = date("Y/m/d", strtotime($date1));    
            $y .= str_replace("/", "", $newDate);
            $y .= "        ";
            $fso = $keywords[3];
            $fso = str_replace('"','',$fso);
            $y .= $keywords[3];
            if($fso != "FPO" || $fso != "APO"){
                $y .= $keywords[4];
            }
            $y = str_replace('"','',$y);
            $storeValue[$count]=$y;
            fwrite($myfile, $y."\n");
            $count++;
    }
    $str = strval($count);
    $strlen = strlen($str);
    if($strlen == 1){
        $footer = 900000;
    }
    else if($strlen == 2){
        $footer = 90000;
    }
    else if($strlen == 3){
        $footer = 9000;
    }
    else if($strlen == 4){
        $footer = 900;
    }
    else if($strlen == 5){
        $footer = 90;
    }
    else{
        $footer = 9;
    }
    $footer .= $count;
    $footerDate = date("Y/m/d");
    $footer .= str_replace("/", "", $footerDate);
    fwrite($myfile, $footer."\n");
    fclose($file);
}
if (file_exists($fileCreate)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($fileCreate));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($fileCreate));
    ob_clean();
    flush();
    readfile($fileCreate);
    exit;
}
  }
?>

UPDATE: It's fixed by adding output_buffering = ON in php.ini file

Two things.
1) You need to start session immediately after the <?php
2) You session redirect does not work if something output before the session redirect (header location).

What you can do is, You can use javascript for redirecting.

echo '<script>window.location.replace("http://stackoverflow.com");</script>';

Anyway if you are using sessions, start session on the top of page ( session_start() ).

<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);
if(!empty($_FILES['uploaded_file']))

Update

It's fixed by adding output_buffering = ON in php.ini file

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