0

On updating my phpspreadsheet in composer to version v1.17.1 my code stopped working. I get a Warning "Cannot modify header information - headers already sent by" message instead. Why is phpspreadsheet changing the headers so I can't call the header command?

The below code convert html to xlsx then download the file. And it always worked until I updated. When I revert back to v1.16.0 the below code works.

$filename = '../tmp/' . $row['FileStoreID'] . '.' . $row['FileExtension'];
$todirectoryname = '../tmp/' . $row['FileStoreID'] . '/';
$filenameonly = $row['TransactionTypeName'] . ' - ' . $row['GroupingName']  . '.xlsx';
$tofilename = $todirectoryname . $filenameonly;
if (!is_dir($todirectoryname)) {
   mkdir($todirectoryname);
}
file_put_contents($filename,$documentdata);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Html');
$spreadsheet = $reader->load($filename);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($tofilename);
header("Location: " . serverurl . '/tmp/' . $tofilename);

The "$writer->save($tofilename);" part of the code works because my temp file is created. But the "header("Location: " . serverurl . '/tmp/' . $tofilename);" do not work.

I have tried all the other suggestions to download phpspreadsheet files. But none of them worked.

See attached the screenshot of what the screen displays. It should not display anything it should download the created file. enter image description here

Niel Buys
  • 75
  • 1
  • 9
  • It sounds like you should take this up with the developer of `phpSpreadsheet`, he does hang out on here, but maybe drop an issue on the [github](https://github.com/PHPOffice/PhpSpreadsheet/issues) – RiggsFolly Apr 20 '21 at 08:21
  • I have created an issue on github there is no response. https://github.com/PHPOffice/PhpSpreadsheet/issues/2000 – Niel Buys Apr 20 '21 at 08:22
  • Maybe if you fixed the Notices. What is your PHP error reporting set to – RiggsFolly Apr 20 '21 at 08:23
  • Now this question is closed so no help from community to write the phpspreadsheet code in a different way, that I don't get error with phpspreadsheet. I have already tried the linked solution I couldn't get it to work because phpspreadsheet is doing the header in there software. – Niel Buys Apr 20 '21 at 08:25
  • I can suppress the messages but my header line is not activating so the download is not triggering. – Niel Buys Apr 20 '21 at 08:26
  • Do you get the **Notices** when you switch back to `v1.16.0` – RiggsFolly Apr 20 '21 at 08:27
  • There is no notices in v1.16.0 – Niel Buys Apr 20 '21 at 08:28
  • Then I think it is those that are being sent to your browser which are therefore making you get the Headers already sent error. Fix those Warnings and you may well see it all works again – RiggsFolly Apr 20 '21 at 08:29
  • Thanks I will see if I can fix phpspreadsheet and submit the pull request. – Niel Buys Apr 20 '21 at 08:31
  • I would bet that the issue is in something you are passing to that function, rather than there being something wrong with phpSpreadsheet. Otherwise there would be hundereds of error reports – RiggsFolly Apr 20 '21 at 08:33
  • Thanks for the help. The code above is the only interaction with phpspreadsheet. The $documentdata is a html report generated by our other software. – Niel Buys Apr 20 '21 at 08:37
  • Did you also change version of PHP? – RiggsFolly Apr 20 '21 at 08:37
  • Then it is likely something being picked up by the code from the HTML, they could argue that is data you are passing to them :) – RiggsFolly Apr 20 '21 at 08:38
  • I changed from php 7.2 to php 7.4 about 5 months ago so not recently. The phpspreadsheet update was a week ago. – Niel Buys Apr 20 '21 at 08:39
  • Thanks I will post what I found on the issue. – Niel Buys Apr 20 '21 at 08:41
  • If you have a debugging env setup (XDEBUG) I would trace the code through and see what is causing those `Non well formed numeric` messages – RiggsFolly Apr 20 '21 at 08:45
  • Ok I confirmed, the new notices "non well formed numeric value" that phpspreadsheet started to show from v1.17.1 is the problem. I added the line "ini_set('display_errors', 0);" before the phpspreadsheet lines in the code then everything works again. – Niel Buys Apr 21 '21 at 11:26
  • NOTE 1: The html is created by other software so I can't fix the html. But the created spreadsheet file has everything I need so I am fine to suppress the messages. – Niel Buys Apr 21 '21 at 11:26
  • NOTE 2: The production server probably would not have been a problem because it suppress messages by default. I can't by default suppress all messages on my development environment. – Niel Buys Apr 21 '21 at 11:26

0 Answers0