How to upload file in Laravel

Last Updated: April 28, 2022

First, you can see the UploadController at App\Http\Controllers folder and add the following code to it.

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 upload form and action store is used to handle post requests and store the upload file.

Now you can add the following code to 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 following 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');;

Now create a view to upload files.
You can crate upload.blade.php file at resources\view and add the following code to make the file upload form.

<div class="container">
<form action="<?php echo route('post.upload')?>" enctype="multipart/form-data" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleFormControlFile1">Example file input</label> 
<input class="form-control-file" name="upload_file" type="file">
<p><button class="btn btn-primary" type="submit">Submit</button></p>

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

upload file

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

uploaded image


Now it is time to download and show the file in your Laravel project.