-1

When executing the code below it returns the error "warning cannot modify header information - headers already sent by.....".

I have written the array to pull the data in 2 different ways, but still receiving issues.

<?php  
//export.php  
    $connect = mysqli_connect("localhost", "username", "password", "dbname");
    $output = '';
    if(isset($_POST["export"]))
{
        $query = "
        select trim(ifnull(application, 'Grand Total' )) as Application, 
        ifnull(sum(Totalcapacity) ,0.00) as 'Capacity (Gig)'
        from storagedata group by application with rollup
         ";

        $result1 = mysqli_query($connect, $query);

        $result2 = mysqli_query($connect, $query);

        $dataRow = "";

        while($row2 = mysqli_fetch_array($result2))

{
        $dataRow = $dataRow."<tr><td>$row2[0]</td><td>$row2[1]</td></tr>";

}
?>



<html>
<head>

        <title>Returning Total Capacity Per DC</title>

</head>

<body>

<!-- Building table -->
        <table align = 'center' border='2' width='300' height='100'>
            <tr>
            <td colspan='2' align='center' bgcolor='lightblue'><b>Total Capacity by DC</b> </td>
            </tr>
            <tr>
                <th>Datacenter</th>
                <th>Total Capacity (Gig)</th>

            </tr>

            <?php while($row1 = mysqli_fetch_array($result1)):;?>
            <tr>
                <td><?php echo $row1[0];?></td>
                <td><?php echo $row1[1];?></td>

            </tr>
            <?php endwhile;?>
</body>
     <br><br>




<?php

  $output .= '</table>';
  header('Content-Type: application/xlsx;');
  header('Content-Disposition: attachment; filename=download.xlsx');
  echo $output;

  }

?>

When clicking from my export button it should export the data. This same bit of code works for other "reports". The only difference is the select query at that top.

Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    Are the `header()` functions called before any other code is sent to the page? Even a single character of rendered output will send the headers before that code executes. The opening ` – tshimkus May 21 '19 at 10:31
  • I think any error is returned on your code before it returns itself – Sanjun Dev May 21 '19 at 13:59

2 Answers2

0

Place:

header('Content-Type: application/xlsx;');
header('Content-Disposition: attachment; filename=download.xlsx');

At the begining of your script.

<?php
header('Content-Type: application/xlsx;');
header('Content-Disposition: attachment; filename=download.xlsx');

//...

And make sure that

<?php

is at the very beginning of your file and there are no lines or white spaces before it.

You can't change header settings after that your script has sent some output.

Franz
  • 645
  • 1
  • 9
  • 21
  • Thank you!!!! That worked perfectly!!! Placing header('Content-Type: application/xls;'); header('Content-Disposition: attachment; filename=download.xls'); at the beginning of the file under the php – user11533099 May 21 '19 at 11:06
  • Please mark as accepted answer ;-) – Franz May 21 '19 at 21:41
-1

Put this code at the top of PHP file signed in the Warning message.

<?php ob_start(); ?>
Mariano
  • 473
  • 3
  • 12