Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP文本文件自动下载,给出已发送的错误标题_Php - Fatal编程技术网

PHP文本文件自动下载,给出已发送的错误标题

PHP文本文件自动下载,给出已发送的错误标题,php,Php,我正在上载文本文件,然后在转换后对其进行转换。我强制它自动下载转换后的文本文件,但它没有被下载,出现错误警告:无法修改标题信息-标题已发送 强制下载文本文件PHP代码 if (file_exists($fileCreate)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Dispositio

我正在上载文本文件,然后在转换后对其进行转换。我强制它自动下载转换后的文本文件,但它没有被下载,出现错误
警告:无法修改标题信息-标题已发送

强制下载文本文件PHP代码

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;
}
  }
完整的PHP代码

<?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;
}
  }
?>

更新:它通过在php.ini文件中添加output_buffering=ON来修复。两件事。
1) 您需要在
更新后立即启动会话

它通过在php.ini文件中添加
output\u buffering=ON
来修复

echo '<script>window.location.replace("http://stackoverflow.com");</script>';
<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);
if(!empty($_FILES['uploaded_file']))