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.
- Upload into the public folder of Laravel is very easy.
- Upload files into Laravel Storage that is also very easy.
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
- Create a controller using the artisan command in the command terminal as shown below
php artisan make:controller UploadController
- 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.
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
'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.