Skip to content

Reusing a saved file which contained an image crashes #544

@MAKS-dev

Description

@MAKS-dev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions