Laravel 5.4 - Image upload with validation

  122429 views   5 years ago Laravel

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..

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


	<title>Laravel5.4 - Image upload with validation</title>
	<link href='//' rel='stylesheet' type='text/css'>
	<link rel="stylesheet" type="text/css" href="">
	<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>
				<input type="submit" value="Upload" name="submit">

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

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]