CSV file should be readable even if the mime type of the file doesn't match.
When I try to read a CSV file PhpSpreadsheet was unable to read it cause canRead function returns false.
I am getting "text/x-fortran" as a mime type for my CSV file.
You can find CSV file here.
I suggest you should not rely on this function to test the file. I removed the mime type check and it was able to read the file successfully with "," as a Delimiter.
PhpSpreadsheet 1.2.0
I had the same problem.
Call to undefined function PhpOfficePhpSpreadsheetReadermime_content_type() in /vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Csv.php:479
@xsdhy You can fix it by removing following lines from the file.
and replace it with "return true" or if you want to keep the check then you can replace the lines with following.
if(function_exists("mime_content_type")) {
$type = mime_content_type($pFilename);
$supportedTypes = [
'text/csv',
'text/plain',
'inode/x-empty',
];
return in_array($type, $supportedTypes, true);
}
return true;
@ravibpatel ok锛宼hanks
Hopefully the author can fix this bug as soon as possible.
I found that this was not a bug, or that it was not a particularly serious bug.
The reason I got this bug was that a file suffix named excel2007 was changed to XLS.
+1 - same issue here
Instead of editing the source, we just locked our version to
dev-master#fb5f8d4763962f1c464b44b2788871903edc43ed
That version successfully recognizes and imports .csv files with no issues - it's the December 23, 2017 release (fb5f8d4763962f1c464b44b2788871903edc43ed) just prior to the 1.0 release. I could probably have used a newer check-in than that but I don't have time at the moment to work backwards through the check-ins to figure out where this broke.
IMO this is a better solution than directly editing the code.
@ravibpatel, we must test somehow that it is some kind of CSV looking file. Otherwise we are back at #167 where CSV reader tried to parse any kind of file, including binary one, and failed in all sort of miserable ways. What we could do though is trust the file extension if it is CSV and hope for the best. And if it is not, then fallback to mime detection.
@xsdhy your issue is that you miss the PHP extension for mime. You should install it, and we should declare it as a dependency.
@abunch, the change was introduced on 1.2.0, https://github.com/PHPOffice/PhpSpreadsheet/releases/tag/1.2.0. So you should be able to safely pin 1.1.0
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.
Any update on this?
'mime_content_type' in 'canRead' is unsafe. I think you should change this. tsv file must passing.
/src/PhpSpreadsheet/Reader/Csv.php
public function canRead($pFilename)
{
...
// Trust file extension if any
if (strtolower(pathinfo($pFilename, PATHINFO_EXTENSION)) === 'csv' ||
strtolower(pathinfo($pFilename, PATHINFO_EXTENSION)) === 'tsv'
) {
return true;
}
...
Im also having an issue. The problem is not fixed with an early true.
Most helpful comment
'mime_content_type' in 'canRead' is unsafe. I think you should change this. tsv file must passing.
/src/PhpSpreadsheet/Reader/Csv.php