How To Upload An Image To Public Folder In Laravel

How To Upload An Image To Public Folder In Laravel

In this tutorial, we will learn how to upload an image to public folder in Laravel. Usually, there are two ways to upload a file in Laravel.

  1. Upload into the public folder of Laravel is very easy.
  2. Upload files into Laravel Storage that is also very easy.

Also read, How to create laravel project from scratch step by step

Required Steps to upload an image in Laravel

  • Create routes in the web.php file inside the routes folder of the Laravel Application as given below

Route::get('/uploadfile','UploadController@index');

Route::post('/uploadfile','UploadController@store');

  • Create a controller using the artisan command in the command terminal as shown below

php artisan make:controller UploadController

UploadController.php

  • Create a blade file to upload the file inside the resources/views folder of the Laravel Application to upload the file. I have created a blade file name ‘uploadfile.blade.php’.
  • Do not forget to use the enctype=”multipart/form-data” in the form of the blade file.

uploadfile.blade.php

Illustration of the example to upload an image to a public folder in Laravel

  • First of all, we have to create a custom folder inside the public folder in the Laravel application.
  • Next, we will validate the file request to know whether it is a file type or not as shown below

$request->validate([

'product_image.*' => 'mimes:doc,pdf,docx,zip,jpeg,png,jpg,gif,svg',

]);

  • If the request has the file type then we will get the file name using getClientOriginalName() function.
  • Then we will move the file to the folder we have created inside the public folder of Laravel.

Conclusion:- I hope this tutorial will help you to upload files in laravel. If you have any doubt then please leave a comment below.

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.