Laravel File(Image) Upload Example with Validation

Laravel File(Image) Upload Example with Validation

In this post we will give you information about Laravel File(Image) Upload Example with Validation. Hear we will give you detail about Laravel File(Image) Upload Example with ValidationAnd how to use it also give you demo for it if it is necessary.

In this tutorial i want to share with you how to create image uploading in Laravel 5. If you are beginners then you can do that simply. Laravel 5 provide very simple way to create file uploading with proper validation like max file size 2mb, file extention should be jpeg,png,jpg,gif or svg etc. So you can easily also implement this on your laravel application. I give you very easy and simple example that way you can undestand very well. So let’s start from routes define.

1. Define Route

First you have add two route in routes.php file. first one for generate view and second one for post method. so let’s add bellow route in your routes.php file.

app/Http/routes.php

Route::group(['middleware' => 'web'], function () {

Route::get('fileUpload', function () {

return view('fileUpload');

});

Route::post('fileUpload', ['as'=>'fileUpload','uses'=>'HomeController@fileUpload']);

});

2. Add Controller Function

Ok, now we need to add fileUpload() in HomeController.php file. If you don’t have HomeController then you can create new and put bellow code on that file.

app/Http/Controllers/HomeController.php

namespace AppHttpControllers;


use IlluminateHttpRequest;


class HomeController extends Controller

{

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('auth');

}


public function fileUpload(Request $request)

{

$this->validate($request, [

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

]);


$image = $request->file('image');

$input['imagename'] = time().'.'.$image->getClientOriginalExtension();

$destinationPath = public_path('/images');

$image->move($destinationPath, $input['imagename']);


$this->postImage->add($input);


return back()->with('success','Image Upload successful');

}


}


3. Add Blade File

In At Last we require to create view file for image or file uploading. so you can create fileUpload.blade.php and put following code in that file.

resources/views/fileUpload.blade.php

Also see:Laravel – How to convert file(image, audio, video) extension using CloudConvert?

@extends('layouts.app')


@section('content')


@if (count($errors) > 0)

<div >

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


{!! Form::open(array('route' => 'fileUpload','enctype' => 'multipart/form-data')) !!}

<div >

<div >

{!! Form::file('image', array('class' => 'image')) !!}

</div>

<div >

<button type="submit" >Create</button>

</div>

</div>

{!! Form::close() !!}


@endsection

Now you check….

Video

Hope this code and post will helped you for implement Laravel File(Image) Upload Example with Validation. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  80  =  81

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US