File uploads

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