0
0
Laravelframework~30 mins

Uploading files in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Uploading Files in Laravel
📖 Scenario: You are building a simple web application where users can upload profile pictures. You want to create a form to upload an image file and save it on the server using Laravel.
🎯 Goal: Build a Laravel file upload feature that accepts an image file from a form, validates it, and stores it in the storage/app/public directory.
📋 What You'll Learn
Create a form with a file input named profile_picture
Add a route to handle the file upload POST request
Write a controller method to validate and store the uploaded file
Save the uploaded file in the storage/app/public directory with the original filename
💡 Why This Matters
🌍 Real World
Uploading files is common in web apps for user avatars, documents, or media. This project shows how to handle file uploads securely and easily in Laravel.
💼 Career
Many web developer jobs require handling file uploads. Knowing Laravel's file upload process is essential for building user-friendly and secure applications.
Progress0 / 4 steps
1
Create the file upload form
Create a Blade view file with a form that uses POST method and enctype="multipart/form-data". Add a file input with the name profile_picture and a submit button.
Laravel
Need a hint?

Remember to set enctype="multipart/form-data" on the form to allow file uploads.

2
Add the upload route
In routes/web.php, add a POST route for /upload that uses the UploadController and its store method.
Laravel
Need a hint?

Use Route::post with the URL /upload and controller UploadController@store.

3
Write the controller method to validate and store the file
In app/Http/Controllers/UploadController.php, create the store method. Validate the request to require a file named profile_picture that is an image. Then store the file in the public disk preserving the original filename.
Laravel
Need a hint?

Use $request->validate() to check the file is required and an image. Use storeAs to save the file with its original name in the public disk.

4
Complete the file upload feature
Add a redirect back to the form with a success message after storing the file in the store method.
Laravel
Need a hint?

Use redirect()->back()->with() to send the user back with a success message.