Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
Invalid HTML due to readEnding failure.
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 expected behavior?
The HTML is valid and is able to find the end which is nothing more than a > symbol

### What is the current behavior?
Due to size of the file returned from filesize() the fseek is returning no data which causes it to fail. This appears to be due to using the same filename for loadingIntoExisting. The filesize reported is for the original version, but when it is updated with new data filesize does not report the new size. 

### What are the steps to reproduce?
Try to use loadIntoExisting() on a smaller HTML file (in this case the OS is reporting 1,518 bytes, 4k on disk). Filesize returns 19792 as the size and then tries to seek to the offset based off of the sample size (19792 - 2048) which goes past the end of the file.


Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of code that exhibits the issue without relying on an external Excel file or a web server:

```php
<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
                $reader = new \PhpOffice\PhpSpreadsheet\Reader\Html;
                $reader->setSheetIndex($index);
                $reader->loadIntoExisting('/tmp/smallhtml'. $spreadsheet);
```


### Which versions of PhpSpreadsheet and PHP are affected?
Php 7.1.29 / PhpSpreadsheet 1.8

Pull request description:
Invalid Html Due to Cached Filesize
Fix #1107. Clear statcache for file before requesting its size.

This is:

- [x] a bugfix
- [ ] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [x] Changes are covered by unit tests
  - [x] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary

### Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 69d4c9ef0..21033ec6e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
 - SUMIFS Does Not Require xlfn. [Issue #4182](https://github.com/PHPOffice/PhpSpreadsheet/issues/4182) [PR #4186](https://github.com/PHPOffice/PhpSpreadsheet/pull/4186)
 - Image Transparency/Opacity with Html Reader Changes. [Discussion #4117](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4117) [PR #4142](https://github.com/PHPOffice/PhpSpreadsheet/pull/4142)
 - Option to Write Hyperlink Rather Than Label to Csv. [Issue #1412](https://github.com/PHPOffice/PhpSpreadsheet/issues/1412) [PR #4151](https://github.com/PHPOffice/PhpSpreadsheet/pull/4151)
+- Invalid Html Due to Cached Filesize. [Issue #1107](https://github.com/PHPOffice/PhpSpreadsheet/issues/1107) [PR #4184](https://github.com/PHPOffice/PhpSpreadsheet/pull/4184)
 
 ## 2024-09-29 - 3.3.0 (no 3.0.\*, 3.1.\*, 3.2.\*)
 
diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php
index 6e9317e78..a95ed6066 100644
--- a/src/PhpSpreadsheet/Reader/Html.php
+++ b/src/PhpSpreadsheet/Reader/Html.php
@@ -173,6 +173,7 @@ class Html extends BaseReader
         // Phpstan incorrectly flags following line for Php8.2-, corrected in 8.3
         $filename = $meta['uri']; //@phpstan-ignore-line
 
+        clearstatcache(true, $filename);
         $size = (int) filesize($filename);
         if ($size === 0) {
             return '';


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Reader/Html/Issue1107Test.php b/tests/PhpSpreadsheetTests/Reader/Html/Issue1107Test.php
new file mode 100644
index 000000000..866850083
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Reader/Html/Issue1107Test.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
+
+use PhpOffice\PhpSpreadsheet\Reader\Html as HtmlReader;
+use PhpOffice\PhpSpreadsheet\Shared\File;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
+use PHPUnit\Framework\TestCase;
+
+class Issue1107Test extends TestCase
+{
+    private string $outfile = '';
+
+    protected function tearDown(): void
+    {
+        if ($this->outfile !== '') {
+            unlink($this->outfile);
+            $this->outfile = '';
+        }
+    }
+
+    public function testIssue1107(): void
+    {
+        // failure due to cached file size
+        $outstr = str_repeat('a', 1023) . "\n";
+        $allout = str_repeat($outstr, 10);
+        $this->outfile = $outfile = File::temporaryFilename();
+        file_put_contents($outfile, $allout);
+        self::assertSame(10240, filesize($outfile));
+        $spreadsheet = new Spreadsheet();
+        $sheet = $spreadsheet->getActiveSheet();
+        $sheet->getCell('A1')->setValue(1);
+        $writer = new HtmlWriter($spreadsheet);
+        $writer->save($outfile);
+        $spreadsheet->disconnectWorksheets();
+        $reader = new HtmlReader();
+        $spreadsheet2 = $reader->load($outfile);
+        $sheet2 = $spreadsheet2->getActiveSheet();
+        self::assertSame(1, $sheet2->getCell('A1')->getValue());
+
+        $spreadsheet2->disconnectWorksheets();
+    }
+}