Laravel 5.4 – Image upload with validation

Laravel 5.4 – Image upload with validation

In this post we will give you information about Laravel 5.4 – Image upload with validation. Hear we will give you detail about Laravel 5.4 – Image upload with validationAnd how to use it also give you demo for it if it is necessary.

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', '[email protected]');
// for image upload
Route::post('upload', '[email protected]');

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

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


<?php 
namespace AppHttpControllers;

use AppHttpRequests;
use IlluminateHttpRequest;
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

[ADDCODE]


<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 >
		<div >
			<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 >
	                	<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…

Hope this code and post will helped you for implement Laravel 5.4 – Image upload 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

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