欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel validator file excel 验证 excel 文件 csv,xls,xlsx 文件 有大用

最好还是看 /node-admin/11927


Laravel validator and excel files error

I have an input field who allaow peoples to upload files. I want that they can upload, word files like doc, and files like csv,xlsx.

When i try with a .doc no problem at all but when i try with an excel files, the validator fail and say that not the good extension.

Here you can see my code, the two lines of comments was an other solution i have try , and it don't work too :(.

Any help is welcome.

public function postFile(Request $request)
{ //Règle de validation avec les type de fichiers acceptés

 if(isset($request->file)){
//dd($request);
   $validator=Validator::make($request->all(),[
     'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp,application/csv,application/excel,
      application/vnd.ms-excel, application/vnd.msexcel,
      text/csv, text/anytext, text/plain, text/x-c,
      text/comma-separated-values,
      inode/x-empty,
      application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  /*  'extension'  => strtolower($request->file->getClientOriginalExtension()),
     'extension'=>'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp'*/
   ]);
  if ($validator->fails()) {
     return back()
                ->withErrors($validator);
   }

4 Answers 正确答案

Ok my fault. I had try an other solution find on this website and it work. Thanks for help Odin. It was my first question on this website i gonna see if i can help someone now. I poste code for solution in someone need :).

$validator = Validator::make(
  [
      'file'      => $request->file,
      'extension' => strtolower($request->file->getClientOriginalExtension()),
  ],
  [
      'file'          => 'required',
      'extension'      => 'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp',
  ]

);

We have 3 open jobs ♥
Imagine yourself at Pantheon Systems

Use "mimes" when you want to write an extentions (xlsx,doc,docx). In case when use mime-type like application/vnd.ms-excel you must use validation rule mimetype

More mime types: more mime-types

$validator=Validator::make($request->all(),[
 //use this
    'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp'
 //or this
    'file'=>'required|max:50000|mimetypes:application/csv,application/excel,
        application/vnd.ms-excel, application/vnd.msexcel,
        text/csv, text/anytext, text/plain, text/x-c,
        text/comma-separated-values,
        inode/x-empty,
        application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]

    In Laravel you can use validate the file upload with extension using After Hooks. Read more from here!

    $validator->after(function ($validator) use ($request){
        if($this->checkExcelFile($request->file('file')->getClientOriginalExtension()) == false) {
            //return validator with error by file input name
            $validator->errors()->add('file', 'The file must be a file of type: csv, xlsx, xls');
        }
    });
    
    function checkExcelFile($file_ext){
        $valid=array(
            'csv','xls','xlsx' // add your extensions here.
        );        
        return in_array($file_ext,$valid) ? true : false;
    }

    First tell that this is not the proper solution. But you can try this.

    I've searched also for that and having so much trouble validating excel file and their mimes type is not working for that unfortunately.

    if($request->hasFile('file'))
    {
       $extension = File::extension($request->file->getClientOriginalName());
       if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
          //'Your file is a valid xls or csv file'
       }else {
          //'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!');
       }
    }

    in namspace include must use File;

    You can validate any file by this way, Thanks.

      来自  https://stackoverflow.com/questions/42089659/laravel-validator-and-excel-files-error




      excel file upload validation mime type error.

      PUBLISHED 2 YEARS AGOBYUMEFAROOQ

      Hii am trying to upload excel file i am also validating excel file but facing problem with excel file mime type validation here is code i am using for file

      'file' => 'required|mimes:xls,xlsx'

      i don't know how laravel is checking mime, where i can set missing mimes for excel files in laravel.

      d3xt3r
      d3xt3r
      2 years ago(135,530 XP)

      From the docs

      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

      Also, may be this could helphttps://laracasts.com/discuss/channels/general-discussion/custom-validation-for-xlxs-and-xls

      umefarooq

      it dose not worked for me, i created custom rule to check only excel file. i think there is some problem with mime validation rule or I'm doing something wrong, i have tried txt mime it is not validating txt file.

      kingpabel

      you need to do some changes,see this

      'file'=>'required|mimes:application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      shivactp

      This is also not working for excel validation

      Pleasesign inorcreate an accountto participate in this conversation.

      来自 https://laracasts.com/discuss/channels/general-discussion/excel-file-upload-validation-mime-type-error


      普通分类: