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

File corrupted after writer creation and save #4179

Closed
kboumedal opened this issue Oct 1, 2024 · 7 comments · Fixed by #4180
Closed

File corrupted after writer creation and save #4179

kboumedal opened this issue Oct 1, 2024 · 7 comments · Fixed by #4180

Comments

@kboumedal
Copy link

kboumedal commented Oct 1, 2024

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 current behavior?

File is corrupted after IOFactory::createWriter and save

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
        $spreadsheet = IOFactory::load(
[SimuV1.xlsx](https://github.com/user-attachments/files/17210539/SimuV1.xlsx)
);

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');

$temporaryPathfile = rtrim(sys_get_temp_dir(), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.md5(uniqid().mt_rand());

$writer->save($temporaryPathfile);

### What features do you think are causing the issue

- [ ] Reader
- [x] Writer
- [ ] Styles
- [ ] Data Validations
- [ ] Formula Calculations
- [ ] Charts
- [ ] AutoFilter
- [ ] Form Elements

### Does an issue affect all spreadsheet file formats? If not, which formats are affected?

xlsx

### Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 3.3, PHP8.3
@oleibman
Copy link
Collaborator

oleibman commented Oct 1, 2024

Thank you for the sample file. Problem confirmed. It also happens with PhpSpreadsheet 2.2.0, so probably is not caused by recent changes.

@kboumedal
Copy link
Author

No indeed, I tried to update to the last version to see if the problem was solved but no.

@oleibman
Copy link
Collaborator

oleibman commented Oct 1, 2024

It might take some time to figure this out. One thing that I see in your spreadsheet that I don't see in most others is the use of SVG images, for which we have no formal support. I'm not saying this is causing the problem, but it would be useful to see what happens without them. Is it possible for you to create a copy of your spreadsheet that uses PNG images in place of SVG, and see if the same problem occurs?

@oleibman
Copy link
Collaborator

oleibman commented Oct 1, 2024

Having nothing to do with SVG, there appears to be a duplicate entry for PNG in ContentTypes. I think that is the reason Xlsx thinks the file is corrupt. Researching ...

@oleibman
Copy link
Collaborator

oleibman commented Oct 1, 2024

Yes, that's it. Your sheet has a background image, and the code is not properly accounting for the image's extension already being defined in ContentTypes. Background images are fairly unusual, which is probably why this problem hasn't arisen before. The SVG images will be dropped from the copy, but are otherwise not the source of any problems. Expect a fix in a few days.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Oct 2, 2024
Fix PHPOffice#4179. Sheet has a background image, and the code is not properly accounting for the image's extension already being defined in ContentTypes, resulting in what Excel thinks is a corrupt file.
@oleibman
Copy link
Collaborator

oleibman commented Oct 2, 2024

If you can test against PR 4180, please do so.

@kboumedal
Copy link
Author

Perfect, it works !

Thank you for your quick fix !

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.

2 participants