Laravel File Upload Validation

后端 未结 2 1808
野趣味
野趣味 2020-12-28 14:31

I\'m new to Laravel. I have a form with a File upload function on it. How can I validate their file? I will only allowed Microsoft Word files. Here\'s my validation code.

相关标签:
2条回答
  • 2020-12-28 14:55

    To validate mime type of a file input in Laravel you can use the mimes rule. Remember to match the mime type detected with the actual mime of file you provide. It may vary on different servers.

    For example, you want to enable adding and word document in you form:

    1) in config/mimes.php add the below mime types:

        'doc'  => array('application/msword', 'application/vnd.ms-office'),
        'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'), 
    

    2) in your validation $rules add the following elements:

        'file' => 'required|max:10000|mimes:doc,docx' //a required, max 10000kb, doc or docx file
    
    0 讨论(0)
  • 2020-12-28 15:08

    Try this?

    'file' => 'required|max:10000|mimes:application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    

    You may want to set some custom message for the response though :)

    0 讨论(0)
提交回复
热议问题