How to validate the file name in Laravel 5.4

蹲街弑〆低调 提交于 2019-12-11 04:52:38

问题


I have a form with three input fields. I want to validate the input value before processing them. Where I want to validate the file name before processing it. I use regular expression and alpha_dash. But I got an error for a valid file name. I want my file name only to contain small letter, numbers, underscore and dashes. How can I check the validation of the file name for my file?

Html

 <form action="create" method="POST"  enctype="multipart/form-data">
          {{csrf_field()}}
      <table cellpadding="2" width="20%"  align="center"
         cellspacing="2">

         <tr>
           <td colspan=2>
           <center><font size=4><b>Add the iteams  please</b></font></center>
           </td>
         </tr>

         <tr>
           <td>Heading</td>
           <td><input type="text" name="heading" id="heading" size="30">
           {!! $errors->first('heading', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td>Image</td>
           <td><input type="file" name="image" id="image" size="40">
           {!! $errors->first('image', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td></td>
           <td colspan="2"><input type="submit" name="submit" value="Add Item" /></td>
         </tr>
     </table>
 </form>

controller part

  1. Using Regular expression format:

- I got error message, “ The image format is invalid” .

 public function store(){
    $this->validate(request(),[
          'heading'=>'required',
          'contentbody'=>'required',
           ‘image'=>['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048','regex:/^[a-z0-9-_]+$/' ]

        ]);

}
  1. using Alpa_dash:

- I got error message, “The image may only contain letters, numbers, and dashes” .

public function store(){
 $this->validate(request(),[
           'heading'=>'required',
          'contentbody'=>'required',
          'image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|alpha_dash'
}

Please help, Thank you!


回答1:


You need to use custom regex I reckon

^[a-z0-9_.-]*$

and in your validation use like this

public function store(){
    $this->validate(request(),[
          'heading'=>'required',
          'contentbody'=>'required',
           ‘image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|regex:/^[a-z0-9_.-]*$/'

        ]);

Edit 2:

I am not sure but as per docs laravel supports only the mentioned format. however I found no issue with jpg when I used validation for images but still lets this out too..

docs

EDIT 3

for custom validation see here




回答2:


If somebody else has the same problem like me. I solved my problem by changing the filename to the current time-stamp instead of using the original filename. That way, I don't need to be worried about the validation of the original filename to be saved in the database.

 public function store(Request $request)
    {
        $this->validate($request,[
          'heading'=>'required',
          'contentbody'=>'required',
          'image'=>['required','image','mimes:jpg,png,jpeg,gif,svg','max:2048']

        ]);

         if($request->hasFile('image')){

            $inputimagename= time().'.'.$request->file('image')->getClientOriginalExtension();
            $request->image->storeAs('public/upload', $inputimagename);

          Post::create([
             'heading'=>request('heading'),
             'content'=>request('contentbody'),
               'image'=>$inputimagename,

            ]);


         }

       return redirect('/home');
    }


来源:https://stackoverflow.com/questions/46419738/how-to-validate-the-file-name-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!