Skip to content

Commit 4cd1d70

Browse files
authored
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
1 parent f426889 commit 4cd1d70

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ and this project adheres to [Semantic Versioning](https://semver.org).
8282

8383
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.
8484
- 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)
85+
- 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.
8586
- 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)
8687

87-
8888
## 1.22.0 - 2022-02-18
8989

9090
### Added

src/PhpSpreadsheet/Reader/Xlsx.php

+3
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ private function getFromZipArchive(ZipArchive $archive, $fileName = '')
368368
if (strpos($fileName, '//') !== false) {
369369
$fileName = substr($fileName, strpos($fileName, '//') + 1);
370370
}
371+
// Relative paths generated by dirname($filename) when $filename
372+
// has no path (i.e.files in root of the zip archive)
373+
$fileName = (string) preg_replace('/^\.\//', '', $fileName);
371374
$fileName = File::realpath($fileName);
372375

373376
// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class XlsxRootZipFilesTest extends TestCase
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private static $testbook = 'tests/data/Reader/XLSX/rootZipFiles.xlsx';
14+
15+
public function testXlsxRootZipFiles(): void
16+
{
17+
$filename = self::$testbook;
18+
$reader = new Xlsx();
19+
$spreadsheet = $reader->load($filename);
20+
$sheet = $spreadsheet->getActiveSheet();
21+
$value = $sheet->getCell('A1')->getValue();
22+
self::assertSame('TEST CELL', $value->getPlainText());
23+
}
24+
}
3.28 KB
Binary file not shown.

0 commit comments

Comments
 (0)