Skip to content

Commit 7b25b18

Browse files
authored
Merge pull request #4040 from oleibman/issue4036
Csv Reader Allow Use of mimetype=text/html Files Without Extension
2 parents 6641597 + 72f53de commit 7b25b18

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
2929
- POWER Null/Bool Args. [PR #4031](https://github.com/PHPOffice/PhpSpreadsheet/pull/4031)
3030
- Do Not Output Alignment and Protection for Conditional Format. [Issue #4025](https://github.com/PHPOffice/PhpSpreadsheet/issues/4025) [PR #4027](https://github.com/PHPOffice/PhpSpreadsheet/pull/4027)
3131
- Xls Conditional Format Improvements. [PR #4030](https://github.com/PHPOffice/PhpSpreadsheet/pull/4030)
32+
- Csv Reader allow use of html mimetype. [Issue #4036](https://github.com/PHPOffice/PhpSpreadsheet/issues/4036) [PR #4049](https://github.com/PHPOffice/PhpSpreadsheet/pull/4040)
3233

3334
## 2024-05-11 - 2.1.0
3435

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ public function canRead(string $filename): bool
566566
'text/csv',
567567
'text/plain',
568568
'inode/x-empty',
569+
'text/html',
569570
];
570571

571572
return in_array($type, $supportedTypes, true);

src/PhpSpreadsheet/Writer/ZipStream3.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Writer;
44

5-
use ZipStream\Option\Archive;
65
use ZipStream\ZipStream;
76

87
class ZipStream3

tests/PhpSpreadsheetTests/IOFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public static function providerIdentify(): array
103103
//['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
104104
['samples/templates/46readHtml.html', 'Html', Reader\Html::class],
105105
['tests/data/Reader/CSV/encoding.utf8bom.csv', 'Csv', Reader\Csv::class],
106+
['tests/data/Reader/HTML/charset.UTF-16.lebom.html', 'Html', Reader\Html::class],
107+
['tests/data/Reader/HTML/charset.UTF-8.bom.html', 'Html', Reader\Html::class],
106108
];
107109
}
108110

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
6+
7+
use PhpOffice\PhpSpreadsheet\IOFactory;
8+
use PhpOffice\PhpSpreadsheet\Reader\Csv as CsvReader;
9+
use PhpOffice\PhpSpreadsheet\Shared\File;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class NotHtmlTest extends TestCase
13+
{
14+
private string $tempFile = '';
15+
16+
protected function tearDown(): void
17+
{
18+
if ($this->tempFile !== '') {
19+
unlink($this->tempFile);
20+
$this->tempFile = '';
21+
}
22+
}
23+
24+
public function testHtmlCantRead(): void
25+
{
26+
// This test has a file which IOFactory will identify as Csv.
27+
// So file can be read using either Csv Reader or IOFactory.
28+
$this->tempFile = $filename = File::temporaryFilename();
29+
$cells = [
30+
['1', '<a href="http://example.com">example</a>', '3'],
31+
['4', '5', '6'],
32+
];
33+
$handle = fopen($filename, 'wb');
34+
self::assertNotFalse($handle);
35+
foreach ($cells as $row) {
36+
fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n");
37+
}
38+
fclose($handle);
39+
// Php8.3- identify file as text/html.
40+
// Php8.4+ identify file as text/csv, and this type of change
41+
// has been known to be retrofitted to prior versions.
42+
$mime = mime_content_type($filename);
43+
if ($mime !== 'text/csv') {
44+
self::assertSame('text/html', $mime);
45+
}
46+
self::assertSame('Csv', IOFactory::identify($filename));
47+
$reader = new CsvReader();
48+
$spreadsheet = $reader->load($filename);
49+
$sheet = $spreadsheet->getActiveSheet();
50+
self::assertSame($cells, $sheet->toArray());
51+
$spreadsheet->disconnectWorksheets();
52+
}
53+
54+
public function testHtmlCanRead(): void
55+
{
56+
// This test has a file which IOFactory will identify as Html.
57+
// So file has to be read using Csv Reader, not IOFactory.
58+
$this->tempFile = $filename = File::temporaryFilename();
59+
$cells = [
60+
['<a href="http://example.com">example</a>', '<div>hello', '3'],
61+
['4', '5', '</div>'],
62+
];
63+
$handle = fopen($filename, 'wb');
64+
self::assertNotFalse($handle);
65+
foreach ($cells as $row) {
66+
fwrite($handle, "{$row[0]},{$row[1]},{$row[2]}\n");
67+
}
68+
fclose($handle);
69+
// Php8.3- identify file as text/html.
70+
// Php8.4+ identify file as text/csv, and this type of change
71+
// has been known to be retrofitted to prior versions.
72+
$mime = mime_content_type($filename);
73+
if ($mime !== 'text/csv') {
74+
self::assertSame('text/html', $mime);
75+
}
76+
self::assertSame('Html', IOFactory::identify($filename));
77+
$reader = new CsvReader();
78+
$spreadsheet = $reader->load($filename);
79+
$sheet = $spreadsheet->getActiveSheet();
80+
self::assertSame($cells, $sheet->toArray());
81+
$spreadsheet->disconnectWorksheets();
82+
}
83+
}

0 commit comments

Comments
 (0)