Laravel File Upload Tutorial

Pinterest LinkedIn Tumblr

Today, We are going to create the Laravel File Upload tutorial. Almost, The application has a file upload functionality. Laravel provides an easy interface for File Uploading. So, Today we are going to learn Laravel File Upload Tutorial. We can easily handle file upload by a user. We focus only on the File Upload code in this tutorial. If you are beginners and want to learn File Uploading step by step then Just follow this tutorial.

Laravel File Upload Tutorial

First of all, We are going to create the Form with the Browse/File input element. Let’s open your view file and create the browse button with form like.

@if ($errors->any())
    <div class="alert alert-danger">
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>

<form action="{{ route('') }}" method="POST" enctype="multipart/form-data">

	<div class="form-group">
	    <label for="imageupload">Image</label>
	    <input type="file" name="image" class="form-control-file">
	    <small id="emailHelp" class="form-text text-muted">Select Image file Jpeg, jpg, Png</small>

	<button type="submit" class="btn btn-primary">Submit</button>


Finally, We are ready for the file uploading. Now we need to validate the uploaded file by user and store that image into the directory. So, Let’s open the controller file for uploading and validating image file.

    'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',

$imageName = time().'.'.request()->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);

return redirect()->back()
    ->with('success','Image uploaded successfully.')

As per the above code, First, we validate the file like

  • Selected file must be an image file
  • The File size must be less than 2MB
  • The File type must be JPEG, JPG, PNG

We checked the basic validation file If you want to add more restriction for Image uploading you can write your own validation rules.

The uploaded by a user are stored in the public/images directory. You can easily access the uploaded file from the images folder like.

<img src="{{ asset('images/'.$imageName) }}">

Just replace the $imageName with image name then check the image are visible in a browser or not.

If you face any issue with the file uploading write the comment below.

Write A Comment