This is:
- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
A spreadsheet should be saved and opened multiple times, even if it contains images.
When opening and saving the file again, after it was saved, the following PHP error is generated:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception:
File zip://test-out.xlsx#xl/media/image11.png does not exist in PhpOffice/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php:203
Stack trace:
#0 PhpOffice/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php(129): PhpOffice\PhpSpreadsheet\Writer\Xlsx\ContentTypes->getImageMimeType('zip://test-ou...')
#1 PhpOffice/PhpSpreadsheet/Writer/Xlsx.php(223): PhpOffice\PhpSpreadsheet\Writer\Xlsx\ContentTypes->writeContentTypes(Object(PhpOffice\PhpSpreadsheet\Spreadsheet), false)
#2 test.php(119): PhpOffice\PhpSpreadsheet\Writer\Xlsx->save('test-out.xlsx')
#3 {main} thrown in PhpOffice/PhpSpreadsheet/Writer/Xlsx/ContentTypes.php on line 203
Start a new document with Excel 2016 insert a small PNG image and save the document.
Upload it to webserver.
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
<?php
require __DIR__ . '/vendor/autoload.php';
// Create new Spreadsheet object
$spreadsheet = IOFactory::load('test-in.xls');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('test-out.xlsx');
unset($spreadsheet, $writer); // Or a page reload
$spreadsheet = IOFactory::load('test-out.xls');
// add code that show the issue here...
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('test-out.xlsx');
Just downloaded the lastest version here.
PHP version 7.0.23
We need this feature to add data to an xls(x) file when its comes available, adding to the same file (open - add - save, wait days, repeat). Currently only works without an image.
A fix or workaround idea would be greatly appreciated.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.
This is still an issue.
It should be possible to reuse an PhpSpreadsheet file, also when it has an image.
I cannot reproduce. The following code will write a new file with an image, reload it, and re-write to disk. The final will will be written correctly.
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Generate an image
$gdImage = imagecreatetruecolor(250, 30);
$textColor = imagecolorallocate($gdImage, 255, 255, 255);
imagestring($gdImage, 4, 5, 5, 'Created with PhpSpreadsheet', $textColor);
// Add a drawing to the worksheet
$drawing = new MemoryDrawing();
$drawing->setImageResource($gdImage);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');
$reloaded = IOFactory::load('test.xlsx');
$reloaded->getActiveSheet()->setCellValue('D5', 'foo');
$writer = new Xlsx($reloaded);
$writer->save('test2.xlsx');
Please re-open issue with a Minimal, Complete, and Verifiable example of code that exhibits this issue without relying on an external Excel file.
After additional tests with l found that the error is caused when opening a file and saving it with the same filename:
$spreadsheet = IOFactory::load('test-out.xls');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('test-out.xlsx');
The save will remove the original file causing the code to fail on reading the image.
This is still an issue.
Recently updated to version 1.8.2 on PHP 7.1.25 and retested the above.
A simple solution is to use a temp filename while saving and move it to the actual name at the end.
Most helpful comment
After additional tests with l found that the error is caused when opening a file and saving it with the same filename:
The save will remove the original file causing the code to fail on reading the image.