5

Laravel 5.4 - Image upload with validation

 2 years ago
source link: https://www.laravelcode.com/post/laravel-54-image-upload-with-validation
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Laravel 5.4 - Image upload with validation

  118331 views

  4 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', '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

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

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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK