Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing a saved file which contained an image crashes #544

Closed
MAKS-dev opened this issue Jun 9, 2018 · 7 comments · Fixed by #1462
Closed

Reusing a saved file which contained an image crashes #544

MAKS-dev opened this issue Jun 9, 2018 · 7 comments · Fixed by #1462

Comments

@MAKS-dev
Copy link

MAKS-dev commented Jun 9, 2018

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)

What is the expected behavior?

A spreadsheet should be saved and opened multiple times, even if it contains images.

What is the current behavior?

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

What are the steps to reproduce?

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');

Which versions of PhpSpreadsheet and PHP are affected?

Just downloaded the lastest version here.
PHP version 7.0.23

@MAKS-dev
Copy link
Author

MAKS-dev commented Jul 2, 2018

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.

@stale
Copy link

stale bot commented Aug 31, 2018

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.

@stale stale bot added the stale label Aug 31, 2018
@MAKS-dev
Copy link
Author

This is still an issue.
It should be possible to reuse an PhpSpreadsheet file, also when it has an image.

@stale stale bot removed the stale label Aug 31, 2018
@PowerKiKi
Copy link
Member

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.

@MAKS-dev
Copy link
Author

MAKS-dev commented Oct 7, 2018

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.

@MAKS-dev
Copy link
Author

MAKS-dev commented Sep 4, 2019

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.

@duncanmey
Copy link

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.

Im getting same issue when using save('php://output'). Is there a work around for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants