
In this Post i will show how to upload an image on your project using the Laravel Framework.
Create Route for Upload Form
Route::get('imageuploadform', '[email protected]');
Open config/app.php and add the Input class to aliases:
'aliases' => [ 'Input' => Illuminate\Support\Facades\Input::class, ],
Crate Upload Controller with imageuploadform Function
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Input; class UploadController extends Controller { public function imageuploadform(){ return view('displayform'); } }
then create displayform.blade.php in resources/views/ Folder Inside
<html> <head> <title>Laravel Image Upload</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'; } .main { 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="main"> <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="profileimg" id="profileimg" accept="image/*"> <input type="submit" value="Upload" name="submit"> <input type="hidden" value="{{ csrf_token() }}" name="_token"> </form> </div> </div> </body> </html>
Then, you will want to add the following route
Route::post('upload', '[email protected]');
Create upload function in UploadController for Move Image to Destination Folder
public function upload(Request $request){ /* Image Format Validation */ $this->validate($request, ['profileimg' => 'mimes:jpeg,bmp,png',]); //only allow this type extension file. $image = Input::file('profileimg'); if($image != ""){ $curentitmestap=date("Y-m-d h:i:s"); /* Upload file to Project Folder /public/upload/ */ $destinationPath = public_path().'/upload/'; $extension = $image->getClientOriginalExtension(); $filename=$curentitmestap."-".$extension; $imageupload=Input::file('profileimg')->move($destinationPath, $filename); } else { $filename=NULL; } }
You can use $filename variable for Database Operations.. Now Upload a file and Check your Destination Folder