How to upload file in Laravel

January 23, 2018

We will see how to upload file in Laravel in this tutorial. First I am going to create fresh laravel project and then you crate the controller and view

First you can download the composer and install it

Next you can download the Laravel y running the following command in your terminal

composer.phar global require "laravel/installer"

Now you can create your Laravel project named fileupload

php composer.phar create-project --prefer-dist laravel/laravel fileupload

To give permission to storage folder, you can run following command inside your project folder

sudo chmod -R 777 storage

Now you can run following command to start the server and you can open the application at

php artisan serve

Now I am going to create controller and this controller will have actions index and store.

php artisan make:controller UploadController 

Now you can see the UploadController at App\Http\Controllers folder

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class UploadController extends Controller
    function index(){

    	return view('upload');

    function store(Request $request){


Action index will show you the file uplaod form and action store is used to handle post rquest and store the upload file

Now you can add following code the store action

function store(Request $request){

    		$file = $request->file('upload_file');
        	return Storage::putFile('public',$file);
    		echo "No File Selected";

Above code will save the upload file to storage\app\public location

If you want to upload the files to public\images you can use the followng code

    if ($request->hasFile('upload_file')) {
        $image = $request->file('upload_file');
        $name = $user->id.'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);

Now you can create the routes for actions. You can add the following code to routes\web.php

Route::get('upload', 'UploadController@index');
Route::post('upload', 'UploadController@store')->name('post.upload');;

Next, we will create the view to upload file.
You can crate upload.blade.php file at resourcs\view and add the following code to make the file upload form



{{ csrf_field() }}

Now you can open the file uplaod page in http://localhost:8000/upload and upload the file

Once you upload the file you can see it in the public folder as shown in below image