Skip to content

Commit bfd7dc8

Browse files
committed
Recognize "New" Mimetype for Empty File
Fix #4521. Reader/Csv (which can be invoked by IOFactory::read), checks the mimetype of files if they don't have a csv/tsv extension. It allows empty files, whose mimetype was set to `inode/x-empty` for Php5.3.11 through Php7.3.33 (except for 5.4.0). For 5.3.1-5.3.10, 5.4.0, and 7.4.0+, the mimetype is `application/x-empty`. Reader/Csv recognizes the `inode` version but not the `application` version, which this PR adds. The person who issued the report also noted that, for a file consisting of cr-lf, Php8.1.* and Php8.2.* report the mimetype as `application/octet-stream`, whereas all other releases report it as `text/plain`. The behavior of Php8.1/2 seems to be a bug, and I cannot possibly guess all the conditions that might lead to that bug. So I will not fix that problem. Anyone who is adversely affected by it can either add a `csv` extension to the filename, or upgrade to Php8.3+, or pre-process the file (e.g. to make it truly empty) before passing it to PhpSpreadsheet. A test case to document the problem is added for documentary purposes; the test will be skipped for Php8.1/2, but run for all other releases.
1 parent 747ccd1 commit bfd7dc8

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ public function canRead(string $filename): bool
605605
'text/csv',
606606
'text/plain',
607607
'inode/x-empty',
608+
'application/x-empty', // has now replaced previous
608609
'text/html',
609610
];
610611

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests;
4+
5+
use PhpOffice\PhpSpreadsheet\IOFactory;
6+
use PhpOffice\PhpSpreadsheet\Shared\File;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class Issue4521Test extends TestCase
10+
{
11+
private string $outfile = '';
12+
13+
protected int $weirdMimetypeMajor = 8;
14+
15+
protected int $weirdMimetypeMinor1 = 1;
16+
17+
protected int $weirdMimetypeMinor2 = 2;
18+
19+
protected function tearDown(): void
20+
{
21+
if ($this->outfile !== '') {
22+
unlink($this->outfile);
23+
$this->outfile = '';
24+
}
25+
}
26+
27+
public function testEmptyFile(): void
28+
{
29+
$this->outfile = File::temporaryFilename();
30+
file_put_contents($this->outfile, '');
31+
$spreadsheet = IOFactory::load($this->outfile);
32+
$sheet = $spreadsheet->getActiveSheet();
33+
self::assertSame('A', $sheet->getHighestColumn());
34+
self::assertSame(1, $sheet->getHighestRow());
35+
$spreadsheet->disconnectWorksheets();
36+
}
37+
38+
public function testCrlfFile(): void
39+
{
40+
if (PHP_MAJOR_VERSION === $this->weirdMimetypeMajor) {
41+
if (
42+
PHP_MINOR_VERSION === $this->weirdMimetypeMinor1
43+
|| PHP_MINOR_VERSION === $this->weirdMimetypeMinor2
44+
) {
45+
self::markTestSkipped('Php mimetype bug with this release');
46+
}
47+
}
48+
$this->outfile = File::temporaryFilename();
49+
file_put_contents($this->outfile, "\r\n");
50+
$spreadsheet = IOFactory::load($this->outfile);
51+
$sheet = $spreadsheet->getActiveSheet();
52+
self::assertSame('A', $sheet->getHighestColumn());
53+
self::assertSame(1, $sheet->getHighestRow());
54+
$spreadsheet->disconnectWorksheets();
55+
}
56+
}

0 commit comments

Comments
 (0)