Laravel 5.4 - Image upload with validation

Today we are share with you one of the simple solution which very helpfull for laravel begginers how to image upload in laravel with validation. laravel provide a very simple functionality for image upload and make easy validation on it.

We are show you how to image upload in laravel with validation step by step

Laravel provide folowing validation for image upload


// check type
'image' => 'mimes:jpeg,bmp,png',
// check size
'image' => 'max:2048',

Now, we are show you how to image upload in laravel with validation in very easy way

step : 1 Create following two route in routes/web.php file

First of fully we need to two route one for upload view and second one is for upload image post request like that..


// for image upload view
Route::post('upload', 'UploadController@view');
// for image upload
Route::post('upload', 'UploadController@upload');

step : 2 Create UploadController in app/Http/Controllers folders

Now, we are create controller for image upload in above path like that..


<?php 
namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Input;
use Validator;
use Redirect;
use View;

class UploadController extends Controller {

	public function view() {
		return view('imageUpload');
	}

	public function upload(Request $request) {
		$this->validate($request, [
	    	'image' => 'mimes:jpeg,bmp,png', //only allow this type extension file.
		]);

		$file = $request->file('image');
		// image upload in public/upload folder.
		$file->move('uploads', $file->getClientOriginalName()); 
		echo 'Image Uploaded Successfully';
	}
}

We are done write code in UploadController for image upload with validation. if in case our validation is make false then laravel request control not upload image and it redirect back in our image upload view file with error and in this view file we are handlle this validation error.

Now, we are create one view/blade file in resources/views folders like that..

step : 3 Create imageUpload.blade.php file in resources/views folders


<html>
<head>
	<title>Laravel5.4 - Image upload with validation</title>
	<link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
	<div class="container">
		<div class="content">
			<h1>File Upload</h1>
			<form action="{{ URL::to('upload') }}" method="post" enctype="multipart/form-data">
				<label>Select image to upload:</label>
				<input type="hidden" value="{{ csrf_token() }}" name="_token">
				<input type="file" name="image" id="file">
				@if ($errors->has('image'))
	            	<span class="help-block">
	                	<strong>{{ $errors->first('image') }}</strong>
	            	</span>
	        	@endif
				<input type="submit" value="Upload" name="submit">
			</form>
		</div>
	</div>
</body>
</html>

We are hope this article is helpfull to you...

 Prev post

Next post