I am using this library to create .xlsx
files from my PHP
script.
Following this guide, I've tried exporting some data to a file called hello.xlsx
.
However, this errors with the below messages:
Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: Could not close zip file hello.xlsx. in vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php on line 399
PhpOffice\PhpSpreadsheet\Writer\Exception: Could not close zip file hello.xlsx. in /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php on line 399
The file doesn't exist pre-execution so I kind of get the errors. I could always try to touch hello.xlsx
and change my script to read that file and append the data, but really, I'm trying to write a new file each time the script is run.
Here is my (simplified) code:
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'foo');
$sheet->setCellValue('B1', 'bar');
$sheet->setCellValue('C1', 'foobar');
$sheet->setCellValue('D1', 'trey is the best');
$writer = new Xlsx($spreadsheet);
$writer->save('hello.xlsx');
Going to those lines in the error show this snippet o' code:
// Close file
if ($zip->close() === false) {
throw new WriterException("Could not close zip file $pFilename.");
}
To me, that isn't exactly a helpful message to debug what's going wrong.
I've found various SO posts on this:
The accepted answer unfortunately didn't work (changed code to $writer->save($_SERVER['DOCUMENT_ROOT']. '/hello.xlsx')
).
I also tried the SaveFiaTempFile
function but that just outputs a bunch of funny characters.
I also don't want to set my folder/file permissions to 777 as that's insane in the membrane.
This accepted answer doesn't really suggest what the correct thing to do is IMO other than change ownerships, but .. change what to what?
So how do I go about fixing/debugging the issue without much to really go on?
Thanks :)
UPDATE
So I thought I'd double check something, I made a folder in my project_root called tmp
. I changed my code to write to $_SERVER['DOCUMENT_ROOT']. '/tmp/hello.xlsx'
and it still gave out the errors.
However, I thought "Hey, let's just open it for jokes" and to my surprise the data is all there, everything seems OK.. but for whatever reason, it still gets the error instead of coming up with the download popup in my browser ..
I found the solution to create dynamic Xlsx files:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="hello.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
Here, we literally just redirect the browser to the php://output
and download the file.