1

I'm using PhpOffice\PhpSpreadsheet to read a csv file like :

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($file);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($file);

It error: Warning: mime_content_type(): Can only process string or stream arguments.

But it works well with xlsx file.

I tried use PhpOffice\PhpSpreadsheet\Reader\Csv but it's same error. I tried dump $file, I see mimeType is text/csv, but when $file->getMimeType() is return text/plain. This makes me cannot validate the upload file (I just allow user upload xlsx/csv file). I think it maybe cause error cannot read csv file?

Edit: It is my fault. load() receive arguments as a string but I pass a Symfony\Component\HttpFoundation\File\UploadFile (because it's work with xlsx file). So change $file to $file->getPathName() and it works well. But with validate file, I think I have to accept text/plain, does anyone has any idea?

1 Answers1

0

You can check the file MIME type with the $file->getClientMimeType() method. This will give you the "text/csv" string you are looking for.

Kegan VanSickle
  • 239
  • 5
  • 3