L
L
Leonid Nimchenko2018-03-03 13:45:48
PHP
Leonid Nimchenko, 2018-03-03 13:45:48

How to upload a file from PHPExcel?

Hello. Using PHPExcel I'm trying to upload a file to the user:

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');

The file is unloaded but when opened it gives an error. If I do not upload the same file, but save it to disk:
$objWriter = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$objWriter->save("c:\\l\\".$filename);

the file opens without problems. The content of the uploaded file differs from the saved one by the presence of one space at the very beginning. If you remove this space with a text editor, then Excel opens the file without problems.
I investigated the save method and found that in both cases it creates a file on disk, but if it needs to be unloaded, it issues it with the command:
copy($pFilename, $originalFilename)
where $pFilename is a temporary file, $originalFilename is 'php://output'.
Can you please tell me where, in this case, a space can be added to the beginning of the file? And how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2018-03-03
@lculver

Look for this space somewhere in your script. For example, next to<?php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question