Uploading files is a common task in many web applications. Laravel 12 makes it incredibly simple with its built-in storage system. In this post, we'll learn how to upload and store a file (like an image or document) using Laravel 12 and display it on the frontend.
✅ What You’ll Learn
- Laravel 12 project setup
- Configure storage folder and symbolic link
- Create model, migration, controller
- Upload files to storage/app/public
- Display uploaded files
🧰 Step 1: Install Laravel 12 Project
If you haven’t already installed Laravel 12:
Start the development server:
⚙️ Step 2: Configure Storage Disk
Laravel uses the filesystems.php
config file to define where files are stored.
Open config/filesystems.php
and ensure the public
disk is defined like this:
Now create the symbolic link from public/storage
to storage/app/public
:
This allows access to uploaded files via the browser.
🗃️ Step 3: Create Model and Migration
We’ll create a model named File
to store file info in the database.
Open the generated migration file in database/migrations/
and update it:
Run the migration:
🧭 Step 4: Create Routes
Open routes/web.php
and add the following routes:
🧑💻 Step 5: Create Controller
Generate a controller:
Then update the app/Http/Controllers/FileController.php
with:
🧾 Step 6: Create Blade Views
1. resources/views/file/create.blade.php
2. resources/views/file/show.blade.php
🧪 Step 7: Test the Application
Run the Laravel server if not already:
Visit:
🔗 http://localhost:8000/file/create
- Select an image or PDF to upload
- On success, you'll be redirected to a preview page
📌 Notes & Best Practices
- Uploaded files are stored in:
storage/app/public/uploads/
- Accessible publicly via:
public/storage/uploads/...
- You can customize file storage disk and path based on needs
- Always validate file type and size for security
✅ Conclusion
You’ve successfully built a file upload system in Laravel 12 that:
-
Validates the file
-
Uploads it to the storage folder
-
Saves file info to database
-
Displays the uploaded file