title: Strange error regarding Warning: Cannot modify header information - headers already sent by when there is no white space PHP
body: <p>I am trying to download CSV file using Header function but due to some reasons it is not downloading in browser.</p>
  
  <p>Error is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>My <code>header.php file line 156</code> which error refers to have error is look like this:</p>
  
  <pre><code>&lt;div class="container-fluid"&gt;
  &lt;div class="row-fluid"&gt;
  &lt;?php if($title[0] != 'reports' &amp;&amp; $title[0] != 'damage_reports' &amp;&amp; $title[0] != 'Byconsignee_reports' &amp;&amp; $title[0] != 'Byhold_reports' &amp;&amp; $title[0] != 'general_reports'){?&gt;
  &lt;div class="span3" id="sidebar"&gt;
    &lt;ul class="nav nav-list bs-docs-sidenav nav-collapse collapse"&gt;
      &lt;?php if($title[0]=='dashboard'){?&gt;
      &lt;li class="active"&gt;&lt;a href="index.php?select=dashboard"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Dashboard&lt;/a&gt;&lt;/li&gt;
      &lt;?php }if($title[0]=='report_entry' || $title[0]=='add_report_entry' || $title[0]=='edit_report_entry'){?&gt;
      &lt;li&gt;&lt;a href="index.php?select=report_entry"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Manage Report Entry&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="index.php?select=add_report_entry"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Add Report Entry&lt;/a&gt;&lt;/li&gt;
      &lt;?php }if($title[0]=='damage_entry' || $title[0]=='add_damage_entry' || $title[0]=='edit_damage_entry'){?&gt;
      &lt;li&gt;&lt;a href="index.php?select=damage_entry"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Manage Damage Entry&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="index.php?select=add_damage_entry"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Add Damage Entry&lt;/a&gt;&lt;/li&gt;
      &lt;?php }if($title[0]=='edit'){?&gt;
      &lt;li&gt;&lt;a href="index.php?select=edit"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Manage Edit&lt;/a&gt;&lt;/li&gt;
      &lt;?php }if($title[0]=='search'){?&gt;
      &lt;li&gt;&lt;a href="index.php?select=search"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Manage Search&lt;/a&gt;&lt;/li&gt;
      &lt;?php }if($title[0]=='users' || $title[0]=='add_user' || $title[0]=='edit_user'){?&gt;
      &lt;li&gt;&lt;a href="index.php?select=users"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Manage User&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="index.php?select=add_user"&gt;&lt;i class="icon-chevron-right"&gt;&lt;/i&gt;Add Users&lt;/a&gt;&lt;/li&gt;
      &lt;?php }?&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  &lt;?php }?&gt;
  </code></pre>
  
  <p>Apprently there is no white space in or even I tried using BOM as well but nothing is working.</p>
  
  <p>Moreover, If i adds <code>ob_start();</code> it only export empty file when the file on ftp is full of data.</p>
  
  <p>Code that is present in the end of my file is - I am using this code to download it in browser.</p>
  
  <pre><code>if ((isset($_REQUEST['date_from']) &amp;&amp; $_REQUEST['date_from'] != "") || (isset($_REQUEST['date_to']) &amp;&amp; $_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-&gt;damage_name . "'  " . $cond . "") or die(mysqli_error(self::get_Conn()));
          $a = mysql_num_rows($sqaal);
          if ($a &gt; 0) {
              fwrite($file, $rows-&gt;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-&gt;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-&gt;damage_name . "'") or die(mysqli_error(self::get_Conn()));
          $a = mysql_num_rows($sqaal);
          if ($a &gt; 0) {
              fwrite($file, $rows-&gt;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-&gt;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));
  </code></pre>
  
  <p>P.S: I have also used hex editor to check for BOM in my code but all code is clean and cleared. Nothing is outputting in header nor it is case in my question as it was not related to <code>echo</code> statement before <code>header</code> function.</p>
  
  <p>My problem is that it is not downloading in browser.</p>