Skip to content

Commit

Permalink
Could not open CSV file containing HTML fragment
Browse files Browse the repository at this point in the history
We now always trust the file extension to avoid false positive of mime
detection for most simple cases. But we still try to guess the mime type
if the file extension does not match or is missing.

Fixes PHPOffice#564
  • Loading branch information
PowerKiKi committed Jun 25, 2018
1 parent edb68ce commit 9fdcaab
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Xlsx reader crashed when reading a file with workbook protection - [#553](https://github.com/PHPOffice/PhpSpreadsheet/pull/553)
- Cell formats with escaped spaces were causing incorrect date formatting - [#557](https://github.com/PHPOffice/PhpSpreadsheet/issues/557)
- Could not open CSV file containing HTML fragment - [##564](https://github.com/PHPOffice/PhpSpreadsheet/issues/564)

## [1.3.1] - 2018-06-12

Expand Down
6 changes: 6 additions & 0 deletions src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ public function canRead($pFilename)

fclose($this->fileHandle);

// Trust file extension if any
if (strtolower(pathinfo($pFilename, PATHINFO_EXTENSION)) === 'csv') {
return true;
}

// Attempt to guess mimetype
$type = mime_content_type($pFilename);
$supportedTypes = [
'text/csv',
Expand Down
2 changes: 2 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function providerCanLoad()
[false, 'data/Reader/Xml/WithoutStyle.xml'],
[true, 'data/Reader/CSV/enclosure.csv'],
[true, 'data/Reader/CSV/semicolon_separated.csv'],
[true, 'data/Reader/CSV/contains_html.csv'],
[true, 'data/Reader/CSV/csv_without_extension'],
[true, 'data/Reader/HTML/csv_with_angle_bracket.csv'],
[true, 'data/Reader/CSV/empty.csv'],
[true, '../samples/Reader/sampleData/example1.csv'],
Expand Down
1 change: 1 addition & 0 deletions tests/data/Reader/CSV/contains_html.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Column A";"<a href=''>Link</a>";"Column C"
3 changes: 3 additions & 0 deletions tests/data/Reader/CSV/csv_without_extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This;Are;Headers
Cell A2;Number with comma;25,5
Two colons and a comma;B|3;:,:

0 comments on commit 9fdcaab

Please sign in to comment.