简体   繁体   English

关于警告的奇怪错误:无法修改标题信息-没有空格PHP时已经发送的标题

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

I am trying to download CSV file using Header function but due to some reasons it is not downloading in browser. 我正在尝试使用Header函数下载CSV文件,但是由于某些原因,它没有在浏览器中下载。

Error is: 错误是:

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

My header.php file line 156 which error refers to have error is look like this: 我的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 }?>

Apprently there is no white space in or even I tried using BOM as well but nothing is working. 显然,其中没有空格,甚至我也尝试过使用BOM,但没有任何效果。

Moreover, If i adds ob_start(); 此外,如果我添加ob_start(); it only export empty file when the file on ftp is full of data. 仅当ftp上的文件已包含数据时,才导出空文件。

Code that is present in the end of my file is - I am using this code to download it in browser. 我文件末尾的代码是-我正在使用此代码在浏览器中下载它。

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

PS: I have also used hex editor to check for BOM in my code but all code is clean and cleared. PS:我还使用了十六进制编辑器来检查代码中的BOM,但是所有代码都是干净的。 Nothing is outputting in header nor it is case in my question as it was not related to echo statement before header function. 标头中没有输出任何内容,在我的问题中也是如此,因为它与header功能之前的echo语句无关。

My problem is that it is not downloading in browser. 我的问题是它没有在浏览器中下载。

Your initial problem is that you are somehow calling header.php , which contains a bunch of html - NO output of any kind can be sent to the browser before the headers. 您最初的问题是,你以某种方式调用header.php ,其中包含了一些HTML的- 没有任何形式的输出可以在标题之前发送给浏览器。 The solution to that is to just call createCSVdamage.php directly. 解决方案是直接调用createCSVdamage.php

Regarding your empty csv, you never actually send the csv data to the oputput stream. 关于空的csv,您实际上从未将csv数据发送到oputput流。 You can do that via readfile function after you set 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);

To move the file you can use the rename and pathinfo functions after sendingh the data: 要移动文件,可以在发送数据后使用重命名pathinfo函数:

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

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

Or alternatively just create the file in the correct directory in the first place! 或者,也可以首先在正确的目录中创建文件!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Filemtime中的PHP警告()&无法修改标头信息 - 标头已发送错误 - PHP Warning in Filemtime() & Cannot modify Header information - Headers Already Sent Error 警告:无法修改标题信息 - 标题已在 wordpress 中发送 - Warning: Cannot modify header information - headers already sent by in wordpress 如何删除警告无法修改标头信息-标头已发送 - How to remove warning Cannot modify header information - headers already sent by 警告:无法修改标头信息-标头已发送 - Warning: Cannot modify header information - headers already sent by Wordpress提要:PHP警告无法修改标题信息-标题已发送 - Wordpress Feeds: PHP Warning Cannot modify header information - headers already sent PHP setcookie()给出“无法修改标题信息 - 标题已发送”警告 - PHP setcookie() gives “Cannot modify header information – headers already sent” warning PHP 重现警告“无法修改 header 信息 - 标头已由(输出开始于...”与 XAMPP 8..1.1 - PHP reproduce warning "Cannot modify header information - headers already sent by (output started at... " with XAMPP 8..1.1 PHP 5.4无法修改标题信息-标题已发送 - Php 5.4 Cannot modify header information - headers already sent PHP - 无法修改标头信息 - 标头已由(输出开始于 - PHP - cannot modify header information - headers already sent by (output started at 无法修改标头信息-标头已经由PHP发送 - Cannot modify header information - headers already sent by: PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM