Skip to content

Commit

Permalink
Read Spreadsheet with # in Name (#2409)
Browse files Browse the repository at this point in the history
Fix #2405. Treat last, rather than first, `#` as separator between zip file name and member name, by finding it with strrpos rather than strpos.
  • Loading branch information
oleibman authored Nov 30, 2021
1 parent 290c18e commit d5825a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Shared/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static function fileExists(string $filename): bool
// doing the original file_exists on ZIP archives...
if (strtolower(substr($filename, 0, 6)) == 'zip://') {
// Open ZIP file and verify if the file exists
$zipFile = substr($filename, 6, strpos($filename, '#') - 6);
$archiveFile = substr($filename, strpos($filename, '#') + 1);
$zipFile = substr($filename, 6, strrpos($filename, '#') - 6);
$archiveFile = substr($filename, strrpos($filename, '#') + 1);

if (self::validateZipFirst4($zipFile)) {
$zip = new ZipArchive();
Expand Down
20 changes: 20 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/OctothorpeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PHPUnit\Framework\TestCase;

class OctothorpeTest extends TestCase
{
public function testOctothorpeInName(): void
{
// Permit # in file name.
$filename = 'tests/data/Reader/XLSX/octo#thorpe.xlsx';
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('xyz', $sheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/octo#thorpe.xlsx
Binary file not shown.

0 comments on commit d5825a6

Please sign in to comment.