diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d09c30b38..3f783d6595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Can read very small HTML files - [#194](https://github.com/PHPOffice/PhpSpreadsheet/issues/194) + ## [1.0.0-beta2] - 2017-11-26 ### Added diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 5df8f49bd6..a6720de68f 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -112,8 +112,6 @@ public function __construct() * * @param string $pFilename * - * @throws Exception - * * @return bool */ public function canRead($pFilename) @@ -148,7 +146,14 @@ private function readEnding() $filename = $meta['uri']; $size = filesize($filename); + if ($size === 0) { + return ''; + } + $blockSize = self::TEST_SAMPLE_SIZE; + if ($size < $blockSize) { + $blockSize = $size; + } fseek($this->fileHandle, $size - $blockSize); diff --git a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php index fa75fac795..9f9b11eb46 100644 --- a/tests/PhpSpreadsheetTests/Reader/HtmlTest.php +++ b/tests/PhpSpreadsheetTests/Reader/HtmlTest.php @@ -13,4 +13,34 @@ public function testCsvWithAngleBracket() $reader = new Html(); self::assertFalse($reader->canRead($filename)); } + + public function providerCanReadVerySmallFile() + { + $padding = str_repeat('a', 2048); + + return [ + [true, ' ' . $padding . ' '], + [true, ' ' . $padding . ''], + [true, ''], + [false, ''], + ]; + } + + /** + * @dataProvider providerCanReadVerySmallFile + * + * @param bool $expected + * @param string $content + */ + public function testCanReadVerySmallFile($expected, $content) + { + $filename = tempnam(sys_get_temp_dir(), 'html'); + file_put_contents($filename, $content); + + $reader = new Html(); + $actual = $reader->canRead($filename); + unlink($filename); + + self::assertSame($expected, $actual); + } }