From 9fdcaabe3cfe49e736e8e92a6700a756776407df Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Mon, 25 Jun 2018 11:12:27 +0900 Subject: [PATCH] Could not open CSV file containing HTML fragment 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 #564 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Reader/Csv.php | 6 ++++++ tests/PhpSpreadsheetTests/Reader/CsvTest.php | 2 ++ tests/data/Reader/CSV/contains_html.csv | 1 + tests/data/Reader/CSV/csv_without_extension | 3 +++ 5 files changed, 13 insertions(+) create mode 100644 tests/data/Reader/CSV/contains_html.csv create mode 100644 tests/data/Reader/CSV/csv_without_extension diff --git a/CHANGELOG.md b/CHANGELOG.md index 72447054..edda25e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/PhpSpreadsheet/Reader/Csv.php b/src/PhpSpreadsheet/Reader/Csv.php index eaece1d2..6899773a 100644 --- a/src/PhpSpreadsheet/Reader/Csv.php +++ b/src/PhpSpreadsheet/Reader/Csv.php @@ -507,6 +507,12 @@ class Csv extends BaseReader 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', diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php index c42b6101..eeddbb08 100644 --- a/tests/PhpSpreadsheetTests/Reader/CsvTest.php +++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php @@ -83,6 +83,8 @@ class CsvTest extends TestCase [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'], diff --git a/tests/data/Reader/CSV/contains_html.csv b/tests/data/Reader/CSV/contains_html.csv new file mode 100644 index 00000000..8f16e027 --- /dev/null +++ b/tests/data/Reader/CSV/contains_html.csv @@ -0,0 +1 @@ +"Column A";"Link";"Column C" \ No newline at end of file diff --git a/tests/data/Reader/CSV/csv_without_extension b/tests/data/Reader/CSV/csv_without_extension new file mode 100644 index 00000000..378dc515 --- /dev/null +++ b/tests/data/Reader/CSV/csv_without_extension @@ -0,0 +1,3 @@ +This;Are;Headers +Cell A2;Number with comma;25,5 +Two colons and a comma;B|3;:,: