Image Uploads with Laravel

Created on June 3rd, 2015

Uploading images with Laravel is stupid easy! In this quick video we'll walk you through a basic example on how to upload an image on your site using the Laravel Framework.

Below you will find the code that we used in the existing resources/views/welcome.blade.php

<html>
	<head>
		<title>Laravel</title>
		
		<link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>

		<style>
			body {
				margin: 0;
				padding: 0;
				width: 100%;
				height: 100%;
				color: #B0BEC5;
				display: table;
				font-weight: 100;
				font-family: 'Lato';
			}

			.container {
				text-align: center;
				display: table-cell;
				vertical-align: middle;
			}

			.content {
				text-align: center;
				display: inline-block;
			}

			.title {
				font-size: 96px;
				margin-bottom: 40px;
			}

			.quote {
				font-size: 24px;
			}

			label{
				margin-right:20px;
			}

			form{
				background:#f5f5f5;
				padding:20px;
				border-radius:10px;
			}

			input[type="submit"]{
				background:#0098cb;
				border:0px;
				border-radius:5px;
				color:#fff;
				padding:10px;
				margin:20px auto;
			}

		</style>
	</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="file" name="file" id="file">
				    <input type="submit" value="Upload" name="submit">
					<input type="hidden" value="{{ csrf_token() }}" name="_token">
				</form>

			</div>
		</div>
	</body>
</html>

Then, you'll want to add the following route to your app/Http/routes.php

Route::post('upload', 'UploadController@upload');

And finally you'll want to create a new Controller inside of app/Http/Controllers called UploadController.php

<?php namespace App\Http\Controllers;

use \Input as Input;

class UploadController extends Controller {

	public function upload(){

		if(Input::hasFile('file')){

			echo 'Uploaded
';
			$file = Input::file('file');
			$file->move('uploads', $file->getClientOriginalName());
			echo '';
		}

	}
}

And that's the basics on how to upload images using Laravel! Couldn't be easier :)

Comments (0)