Fix reading of files in the root of a zip (#2731)

* Fix reading of files in the root of a zip

Xlsx.php relies in dirname($filename) for path generation. When path is a bare filename (i.e. files in the root of the zip file), dirname($filename) returns a relative path to the current directory ("."). This is ok for filesystems, but not when accesing contents in a zip file.

Xlsx documents with files in the root of the zip container are not common, but legit. I've found it to happen in files generated by Google Campaign Manager 360.

* Update Xlsx.php

* Update Xlsx.php

* Update CHANGELOG.md

* Add files via upload

* Create XlsxRootZipFilesTest.php

* Update XlsxRootZipFilesTest.php

* Add files via upload

* Delete rootZipFiles.xlsx

* Update XlsxRootZipFilesTest.php

* Update Xlsx.php
This commit is contained in:
andres1gb 2022-04-18 15:54:41 +02:00 committed by GitHub
parent f426889571
commit 4cd1d7039d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 1 deletions

View File

@ -82,9 +82,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).
Nor is this a perfect solution, as there may still be issues when function calls have array arguments that themselves contain function calls; but it's still better than the current logic.
- Fix for escaping double quotes within a formula [Issue #1971](https://github.com/PHPOffice/PhpSpreadsheet/issues/1971) [PR #2651](https://github.com/PHPOffice/PhpSpreadsheet/pull/2651)
- Fix for reading files in the root directory of a ZipFile, which should not be prefixed by relative paths ("./") as dirname($filename) does by default.
- Fix invalid style of cells in empty columns with columnDimensions and rows with rowDimensions in added external sheet. [PR #2739](https://github.com/PHPOffice/PhpSpreadsheet/pull/2739)
## 1.22.0 - 2022-02-18
### Added

View File

@ -368,6 +368,9 @@ class Xlsx extends BaseReader
if (strpos($fileName, '//') !== false) {
$fileName = substr($fileName, strpos($fileName, '//') + 1);
}
// Relative paths generated by dirname($filename) when $filename
// has no path (i.e.files in root of the zip archive)
$fileName = (string) preg_replace('/^\.\//', '', $fileName);
$fileName = File::realpath($fileName);
// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming

View File

@ -0,0 +1,24 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;
class XlsxRootZipFilesTest extends TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/rootZipFiles.xlsx';
public function testXlsxRootZipFiles(): void
{
$filename = self::$testbook;
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$value = $sheet->getCell('A1')->getValue();
self::assertSame('TEST CELL', $value->getPlainText());
}
}

Binary file not shown.