Shawdow's avatar

How to add the Image validations for form input Laravel

Hi,

I am writing Program on Uploading Image to the database. the written programs works when i upload image to the database and it gets saved to the images folder which is under public.

How to add the Validation for the Image input not to add any other files only images in laravel

in the controller i have written as below

 public function saveImage(Request $request){

        
        $files = Input::file('images');
               
        $file_count = count($files);
        
        $uploadcount = 0;

        foreach ($files as $file) {   
      
                $destinationPath = 'images';
                $filename =  $file->getClientOriginalName();
                $file->move($destinationPath,$filename);
                $uploadcount ++;
               
                $file->getClientOriginalExtension();
                $entry = new \App\image();
                $entry->mime = $file->getClientMimeType();
                $entry->filename = $filename;
                $entry->save();
            
          }
        
        return redirect('/image/upload')->with("success","Uploaded Sucessfully");   
}

in the blade file

   <div class="col-sm-10 well-lg">
            <div class="row">    
                <div class="container-fluid">
                    <div class="form-group">
                      <h2>Image Upload</h2>
                      {!! Form::open(array('url'=>'/image/update','method'=>'POST', 'files'=>true)) !!}
                      {!! Form::file('images[]', array('multiple'=>true)) !!}
                        <p>{!!$errors->first('filename')!!}</p>
                        @if(Session::has('error'))
                          <p>{!! Session::get('error') !!}</p>
                        @endif
                      {!! Form::submit('Submit', array('class'=>'btn btn-lg btn-primary col-md-4')) !!}
                      {!! Form::close() !!}
                    </div>
                </div>
            </div>
           </div>

in the web.php file

Route::get('/image/upload','ImageuploadController@getImage');
Route::post('/image/update','ImageuploadController@saveImage');

0 likes
3 replies
Shawdow's avatar

@Snapey Whoops !! I have tried like below gives me error

Invalid argument supplied for foreach()

 public function saveImage(Request $request){

     $validator = \Validator::make($request->all(), [
       'images[]' => 'required|images',
      ]);
        
        $files = Input::file('images');
               
        $file_count = count($files);
        
        $uploadcount = 0;

        foreach ($files as $file) {   
      
                $destinationPath = 'images';
                $filename =  $file->getClientOriginalName();
                $file->move($destinationPath,$filename);
                $uploadcount ++;
               
                $file->getClientOriginalExtension();
                $entry = new \App\image();
                $entry->mime = $file->getClientMimeType();
                $entry->filename = $filename;
                $entry->save();
            
          }
        
        return redirect('/image/upload')->with("success","Uploaded Sucessfully");   
}
Shawdow's avatar

I have tried in this way of validation

 $this->validate($request,[
              
            'images' => 'required',
        ]);

if i try to upload text file the files gets uploaded

Please or to participate in this conversation.