Skip to content

Commit 2c6983e

Browse files
PowerKiKiFrederic Delaunay
authored and
Frederic Delaunay
committed
Check for MIME type to know if CSV reader can read a file
CSV reader used to accept any file without any kind of check. That made users incorrectly believe that things were ok, even though there is no way for CSV reader to read anything else that plain text files. Fixes PHPOffice#167
1 parent dbe4fd7 commit 2c6983e

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354)
13+
- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167)
1314

1415
## [1.1.0] - 2018-01-28
1516

src/PhpSpreadsheet/Reader/Csv.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ protected function inferSeparator()
195195
}
196196

197197
$meanSquareDeviations[$delimiter] = array_reduce(
198-
$series,
199-
function ($sum, $value) use ($median) {
200-
return $sum + pow($value - $median, 2);
201-
}
202-
) / count($series);
198+
$series,
199+
function ($sum, $value) use ($median) {
200+
return $sum + pow($value - $median, 2);
201+
}
202+
) / count($series);
203203
}
204204

205205
// ... and pick the delimiter with the smallest mean square deviation (in case of ties, the order in potentialDelimiters is respected)
@@ -476,6 +476,13 @@ public function canRead($pFilename)
476476

477477
fclose($this->fileHandle);
478478

479-
return true;
479+
$type = mime_content_type($pFilename);
480+
$supportedTypes = [
481+
'text/csv',
482+
'text/plain',
483+
'inode/x-empty',
484+
];
485+
486+
return in_array($type, $supportedTypes, true);
480487
}
481488
}

tests/PhpSpreadsheetTests/Reader/CsvTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,30 @@ public function providerDelimiterDetection()
6363
],
6464
];
6565
}
66+
67+
/**
68+
* @dataProvider providerCanLoad
69+
*
70+
* @param bool $expected
71+
* @param string $filename
72+
*/
73+
public function testCanLoad($expected, $filename)
74+
{
75+
$reader = new Csv();
76+
self::assertSame($expected, $reader->canRead($filename));
77+
}
78+
79+
public function providerCanLoad()
80+
{
81+
return [
82+
[false, 'data/Reader/Ods/data.ods'],
83+
[false, 'data/Reader/Xml/WithoutStyle.xml'],
84+
[true, 'data/Reader/CSV/enclosure.csv'],
85+
[true, 'data/Reader/CSV/semicolon_separated.csv'],
86+
[true, 'data/Reader/HTML/csv_with_angle_bracket.csv'],
87+
[true, 'data/Reader/CSV/empty.csv'],
88+
[true, '../samples/Reader/sampleData/example1.csv'],
89+
[true, '../samples/Reader/sampleData/example2.csv'],
90+
];
91+
}
6692
}

tests/data/Reader/CSV/empty.csv

Whitespace-only changes.

0 commit comments

Comments
 (0)