Home Laravel Laravel File(Image) Upload Example with Validation

Laravel File(Image) Upload Example with Validation

by Shahriar Sagor

Today now in this tutorial i want to share with you a example of how to create image uploading in Laravel application. If you are a beginners then i hope you can do that very simply. Laravel provide use a very simple way to create the file uploading with proper validation as like max file size 2mb, file extention must be jpeg,png,jpg,gif or svg etc. So now we can do this easily also implement this on our laravel application. Here I will give you a very easy and simple example on that way we can understand very well.

So let’s start from routes define.

1. Define Route

At first we have add two new route in routes.php file. Here first one for generate the view and the second one for post method.

So let’s add the 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

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

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;


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

Finally at Last we need to create the view file for image or file uploading. So we can create a fileUpload.blade.php and then need to put following code in that file.

resources/views/fileUpload.blade.php

@extends('layouts.app')


@section('content')


@if (count($errors) > 0)
	<div class="alert alert-danger">
		<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 class="row cancel">
		<div class="col-md-4">
			{!! Form::file('image', array('class' => 'image')) !!}
		</div>
		<div class="col-md-4">
			<button type="submit" class="btn btn-success">Create</button>
		</div>
	</div>
{!! Form::close() !!}


@endsection

Read Also : How to convert Object into Array in PHP?

Thanks for read. I hope it help you. For more you can follow us on facebook.

close

You may also like