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 #167
This commit is contained in:
Adrien Crivelli 2018-02-05 21:33:23 +09:00
parent 13a10e40a7
commit e31878ceb1
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 40 additions and 6 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed ### Fixed
- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) - Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354)
- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167)
## [1.1.0] - 2018-01-28 ## [1.1.0] - 2018-01-28

View File

@ -476,6 +476,13 @@ class Csv extends BaseReader
fclose($this->fileHandle); fclose($this->fileHandle);
return true; $type = mime_content_type($pFilename);
$supportedTypes = [
'text/csv',
'text/plain',
'inode/x-empty',
];
return in_array($type, $supportedTypes, true);
} }
} }

View File

@ -63,4 +63,30 @@ class CsvTest extends TestCase
], ],
]; ];
} }
/**
* @dataProvider providerCanLoad
*
* @param bool $expected
* @param string $filename
*/
public function testCanLoad($expected, $filename)
{
$reader = new Csv();
self::assertSame($expected, $reader->canRead($filename));
}
public function providerCanLoad()
{
return [
[false, 'data/Reader/Ods/data.ods'],
[false, 'data/Reader/Xml/WithoutStyle.xml'],
[true, 'data/Reader/CSV/enclosure.csv'],
[true, 'data/Reader/CSV/semicolon_separated.csv'],
[true, 'data/Reader/HTML/csv_with_angle_bracket.csv'],
[true, 'data/Reader/CSV/empty.csv'],
[true, '../samples/Reader/sampleData/example1.csv'],
[true, '../samples/Reader/sampleData/example2.csv'],
];
}
} }

View File