XLS files are complex. You can look at some header info to guess. Best bet is to try and open it in PHPOffice Excel. But it does not support every variation of file. Some valid XSLX files contain extra namespace info for certain object types which Excel and Numbers can read but PHPOffice cannot. I had to modify office Excel to strip out that info. Still trying to figure out the proper support to add.
What is the best way to validate Spreadsheet files (xls, xlsx and csv) in Laravel?
Hey you all,
I am working on an application which processes spreadsheets in the following formats: XLS, XLSX and CSV. I want to validate the file that the user selects to upload. I followed Laravel's documentation that says:
Even though you only need to specify the extensions, this rule actually validates against the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
So I went to that link and I found the following MIME types:
MIME type (lowercased) Extensions
============================================ ==========
application/vnd.ms-excel xls xlm xla xlc xlt xlw
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
text/csv csv
I tried with all of them in my code, like:
$this->validate($request,[
'version' => 'required',
'file' => 'required | mimes:application/vnd.ms-excel',
]);
$file = $request->file('file');
if (!($file->isValid()))
{
$error = $file->getErrorsuccess();
return view('spreadsheet/create')->with('error',$error);
}
Besides the MIME types listed on apache's website I also tried with simply 'xls, xlsx, csv' but it didn't work either.
It fails for all the three file extensions and I tried with different files.
How am I supposed to validate those kinds of files? I did a test with png and it worked fine.
Please or to participate in this conversation.