Skip to content

Commit 6382dba

Browse files
committed
Invalid Html Due to Cached Filesize
Fix #1107. Clear statcache for file before requesting its size.
1 parent 87ddd21 commit 6382dba

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/PhpSpreadsheet/Reader/Html.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ private function readEnding(): string
173173
// Phpstan incorrectly flags following line for Php8.2-, corrected in 8.3
174174
$filename = $meta['uri']; //@phpstan-ignore-line
175175

176+
clearstatcache(true, $filename);
176177
$size = (int) filesize($filename);
177178
if ($size === 0) {
178179
return '';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Html as HtmlReader;
8+
use PhpOffice\PhpSpreadsheet\Shared\File;
9+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10+
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class Issue1107Test extends TestCase
14+
{
15+
private string $outfile = '';
16+
17+
protected function tearDown(): void
18+
{
19+
if ($this->outfile !== '') {
20+
unlink($this->outfile);
21+
$this->outfile = '';
22+
}
23+
}
24+
25+
public function testIssue1107(): void
26+
{
27+
// failure due to cached file size
28+
$outstr = str_repeat('a', 1023) . "\n";
29+
$allout = str_repeat($outstr, 10);
30+
$this->outfile = $outfile = File::temporaryFilename();
31+
file_put_contents($outfile, $allout);
32+
self::assertSame(10240, filesize($outfile));
33+
$spreadsheet = new Spreadsheet();
34+
$sheet = $spreadsheet->getActiveSheet();
35+
$sheet->getCell('A1')->setValue(1);
36+
$writer = new HtmlWriter($spreadsheet);
37+
$writer->save($outfile);
38+
$spreadsheet->disconnectWorksheets();
39+
$reader = new HtmlReader();
40+
$spreadsheet2 = $reader->load($outfile);
41+
$sheet2 = $spreadsheet2->getActiveSheet();
42+
self::assertSame(1, $sheet2->getCell('A1')->getValue());
43+
44+
$spreadsheet2->disconnectWorksheets();
45+
}
46+
}

0 commit comments

Comments
 (0)