File uploads

Contents
Validate files
Store uploads
Public disk link
Serve back

Validate files

Never trust uploads blindly: validate size, type, and presence before touching the bytes. One rule line rejects wrong formats early with a clean error message on the form.

$request->validate([ "photo" => "required|image|max:2048", ]);

Store uploads

Uploaded files arrive as file objects on the request. The store method moves each file to a folder on the chosen disk and returns the stored path for the database.

$path = $request->file("photo")->store("photos", "public"); echo $path;

Public disk link

The public disk stays hidden from browsers until one artisan command connects it. The link command creates a shortcut from the public folder to stored files, after which addresses built by Storage open in the browser.

php artisan storage:link

The public disk link has been connected

Serve back

Stored paths turn back into web addresses with the url method. Save the path at upload time, then build the address on every page that shows the file.

use Illuminate\Support\Facades\Storage; $url = Storage::disk("public")->url($path); echo $url;

Next: Eloquent advanced

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: