关于警告的奇怪错误:无法修改 header 信息 - headers 已经在没有白色 space PHP 时发送

Strange error regarding Warning: Cannot modify header information - headers already sent by when there is no white space PHP

我正在尝试使用 Header 函数下载 CSV 文件,但由于某些原因无法在浏览器中下载。

错误是:

Warning: Cannot modify header information - headers already sent by (output started at /xxx/fruitapp/report-admin/common/header.php:156) in /xxx/fruitapp/report-admin/pages/createCSVdamage.php on line 434

我的header.php file line 156哪个错误指的是有错误是这样的:

<div class="container-fluid">
<div class="row-fluid">
<?php if($title[0] != 'reports' && $title[0] != 'damage_reports' && $title[0] != 'Byconsignee_reports' && $title[0] != 'Byhold_reports' && $title[0] != 'general_reports'){?>
<div class="span3" id="sidebar">
  <ul class="nav nav-list bs-docs-sidenav nav-collapse collapse">
    <?php if($title[0]=='dashboard'){?>
    <li class="active"><a href="index.php?select=dashboard"><i class="icon-chevron-right"></i>Dashboard</a></li>
    <?php }if($title[0]=='report_entry' || $title[0]=='add_report_entry' || $title[0]=='edit_report_entry'){?>
    <li><a href="index.php?select=report_entry"><i class="icon-chevron-right"></i>Manage Report Entry</a></li>
    <li><a href="index.php?select=add_report_entry"><i class="icon-chevron-right"></i>Add Report Entry</a></li>
    <?php }if($title[0]=='damage_entry' || $title[0]=='add_damage_entry' || $title[0]=='edit_damage_entry'){?>
    <li><a href="index.php?select=damage_entry"><i class="icon-chevron-right"></i>Manage Damage Entry</a></li>
    <li><a href="index.php?select=add_damage_entry"><i class="icon-chevron-right"></i>Add Damage Entry</a></li>
    <?php }if($title[0]=='edit'){?>
    <li><a href="index.php?select=edit"><i class="icon-chevron-right"></i>Manage Edit</a></li>
    <?php }if($title[0]=='search'){?>
    <li><a href="index.php?select=search"><i class="icon-chevron-right"></i>Manage Search</a></li>
    <?php }if($title[0]=='users' || $title[0]=='add_user' || $title[0]=='edit_user'){?>
    <li><a href="index.php?select=users"><i class="icon-chevron-right"></i>Manage User</a></li>
    <li><a href="index.php?select=add_user"><i class="icon-chevron-right"></i>Add Users</a></li>
    <?php }?>
  </ul>
</div>
<?php }?>

显然没有白色 space 甚至我也尝试使用 BOM 但没有任何效果。

此外,如果我添加 ob_start(); 它只会在 ftp 上的文件充满数据时导出空文件。

我文件末尾的代码是 - 我正在使用此代码在浏览器中下载它。

if ((isset($_REQUEST['date_from']) && $_REQUEST['date_from'] != "") || (isset($_REQUEST['date_to']) && $_REQUEST['date_to'] != "")) {
    fputcsv($file, array(
        'Report Number',
        'Consignee',
        'Pallet ID',
        'Label/Mark',
        'Fruit/Variety',
        'Deck',
        'Type',
        'Damage Description',
        'Pieces Damage',
        'Category'
    ));
    $sql = mysql_query("SELECT * FROM damage") or die('Error In Query');
    while ($rows = mysql_fetch_object($sql)) {
        $sqaal = mysql_query("SELECT
                                                    damage_entry.report_ID,
                                                    damage_entry.consignee,
                                                    damage_entry.pallet_ID,
                                                    damage_entry.label,
                                                    damage_entry.variety,
                                                    CONCAT(damage_entry.hold,damage_entry.deck),
                                                    damage_entry.type,
                                                    damage_entry.damage_desc,
                                                    damage_entry.pieces,
                                                    damage_entry.category_code
                                                FROM
                                                    damage_entry
                                                WHERE
                                                    damage='" . $rows->damage_name . "'  " . $cond . "") or die(mysqli_error(self::get_Conn()));
        $a = mysql_num_rows($sqaal);
        if ($a > 0) {
            fwrite($file, $rows->damage_name);
            fwrite($file, "\r\n");
            $valuesArray = array();
            $as          = 0;
            $sd          = array();
            $gggg        = array();
            while ($row = mysql_fetch_assoc($sqaal)) {
                $array = array(
                    $row['report_ID'],
                    $row['consignee'],
                    $row['pallet_ID'],
                    $row['label'],
                    $row['variety'],
                    $row['CONCAT(damage_entry.hold,damage_entry.deck)'],
                    $row['type'],
                    $row['damage_desc'],
                    $row['pieces'],
                    //$objadminViewFunctions->getCategoryLetter($row['category_code'])
                );
                array_push($sd, $row['pieces']);
                array_push($gggg, $row['pallet_ID']);
                fputcsv($file, $array);
            }
            fwrite($file, "\r\n");
            fputcsv($file, array(
                "",
                "Sub Total",
                count($gggg),
                "",
                "",
                "",
                "",
                "",
                array_sum($sd),
                "",
                ""
            ));
            fwrite($file, "\r\n");
        }
    }
} else {
    fputcsv($file, array(
            'Report Number',
            'Consignee',
            'Pallet ID',
            'Label/Mark',
            'Fruit/Variety',
            'Deck',
            'Type',
            'Damage Description',
            'Pieces Damage',
            'Category'
        ));
    $sql = mysql_query("SELECT * FROM damage") or die('Error In Query');
    while ($rows = mysql_fetch_object($sql)) {
        $sqaal = mysql_query("SELECT
                                                    damage_entry.de_ID,
                                                    damage_entry.report_ID,
                                                    damage_entry.consignee,
                                                    damage_entry.pallet_ID,
                                                    damage_entry.label,
                                                    damage_entry.variety,
                                                    CONCAT(damage_entry.hold,damage_entry.deck),
                                                    damage_entry.type,
                                                    damage_entry.damage_desc,
                                                    damage_entry.pieces,
                                                    damage_entry.category_code
                                                FROM
                                                    damage_entry
                                                WHERE
                                                    damage='" . $rows->damage_name . "'") or die(mysqli_error(self::get_Conn()));
        $a = mysql_num_rows($sqaal);
        if ($a > 0) {
            fwrite($file, $rows->damage_name);
            fwrite($file, "\r\n");
            $valuesArray = array();
            $as          = 0;
            $sd          = array();
            $gggg        = array();
            while ($row = mysql_fetch_assoc($sqaal)) {
                $array = array(
                    $row['report_ID'],
                    $row['consignee'],
                    $row['pallet_ID'],
                    $row['label'],
                    $row['variety'],
                    $row['CONCAT(damage_entry.hold,damage_entry.deck)'],
                    $row['type'],
                    $row['damage_desc'],
                    $row['pieces'],
                    //$objadminViewFunctions->getCategoryLetter($row['category_code'])
                );
                array_push($sd, $row['pieces']);
                array_push($gggg, $row['pallet_ID']);
                fputcsv($file, $array);
            }
            fwrite($file, "\r\n");
            fputcsv($file, array(
                "",
                "Sub Total",
                count($gggg),
                "",
                "",
                "",
                "",
                "",
                array_sum($sd),
                "",
                ""
            ));
            fwrite($file, "\r\n");
        }
    }
}
fclose($file);
$filename = $FileName;
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension ){
    case "pdf": $ctype="application/pdf"; break;
    case "exe": $ctype="application/octet-stream"; break;
    case "zip": $ctype="application/zip"; break;
    case "doc": $ctype="application/msword"; break;
    case "xls": $ctype="application/vnd.ms-excel"; break;
    case "csv": $ctype="text/csv"; break;
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
}
header("Content-Type: application/octet-stream");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

P.S:我还使用了十六进制编辑器来检查代码中的 BOM,但所有代码都是干净且清晰的。 header 中没有任何输出,在我的问题中也没有输出,因为它与 header 函数之前的 echo 语句无关。

我的问题是无法在浏览器中下载。

您最初的问题是您以某种方式调用 header.php,其中包含一堆 html - NO 任何类型的输出都可以发送到headers 之前的浏览器。 解决方案是直接调用 createCSVdamage.php

关于您的空 csv,您实际上从未将 csv 数据发送到输出流。您可以在设置 headers:

后通过 readfile 函数执行此操作
header("Content-Type: application/octet-stream");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

//send the data to the browser
readfile($filename);

要移动文件,您可以在发送数据后使用 rename and pathinfo 函数:

$fileData = pathinfo($filename);
$nameWithoutDirectory = $fileData['basename'];

rename($filename, 'some/directory/' . $nameWithoutDirectory);

或者首先在正确的目录中创建文件!