Storing Files in the Public and Storage Folders
Laravel gives uploaded and generated files a couple of natural homes, and choosing correctly between them affects both security and how easily files can be served to visitors.
The storage Folder
By default, files are stored inside storage/app, which isn't directly accessible from the web. This is the right choice for anything that shouldn't be publicly reachable by guessing a URL, like private documents or user-specific files.
The public Folder
Files that do need to be accessible directly through a URL, like profile pictures or product images, are typically stored using the "public" disk, which points to storage/app/public. A symbolic link connects this folder to the actual public/storage path so the web server can serve it.
| Location | Publicly Accessible? | Typical Use |
|---|---|---|
| storage/app | No, by default | Private documents, internal files |
| storage/app/public | Yes, via symbolic link | Profile photos, product images |
| public/storage | Yes, directly | The publicly served link to the folder above |
Setting Up the Public Link
- The
php artisan storage:linkcommand creates the necessary symbolic link - Without this link, files stored on the public disk won't actually be reachable
- Cloud storage disks, like S3, don't require this local linking step
Manually managing paths across these different locations gets repetitive fast — which is exactly the problem Laravel's Storage facade is designed to solve.
Handling an Upload from Start to Finish
A typical profile photo upload goes through four steps: validate, store, save the path, and display it.
public function updateAvatar(Request $request)
{
$request->validate([
'avatar' => ['required', 'image', 'max:2048'], // size in kilobytes
]);
$path = $request->file('avatar')->store('avatars', 'public');
$request->user()->update(['avatar_path' => $path]);
return back()->with('status', 'Photo updated');
}
The store method generates a unique file name, writes the file under storage/app/public/avatars and returns a relative path such as avatars/abc123.jpg. Save only that relative path in the database, never a full server path. To show the image in Blade:
<img src="{{ Storage::url($user->avatar_path) }}" alt="Profile photo">
This works only after php artisan storage:link has created the link from public/storage.
Serving Private Files Safely
An invoice or ID document should go to the private local disk instead, and reach the user only through a controller that checks permission first:
$path = $request->file('invoice')->store('invoices'); // default private disk
public function download(Invoice $invoice)
{
Gate::authorize('view', $invoice);
return Storage::download($invoice->file_path);
}
Because the file sits outside the web root, nobody can fetch it by guessing a URL. The controller becomes the only door, so every download passes through your authorization rules and can be logged if needed. Some drivers also support temporary signed URLs, which expire after a set time and are a good fit for cloud storage.
Where Disks Are Configured
Disks are defined in config/filesystems.php. Each entry names a driver (local, s3 and so on), a root folder and, for public disks, a URL. Recent Laravel versions point the default local disk at storage/app/private, while older projects use storage/app directly, so check your own config file before assuming a location. Switching from local storage to a cloud bucket later is mostly a config change, because your code talks to a disk name rather than a folder.
Common Mistakes with File Storage
- Putting uploads directly in the
publicfolder. They bypass Laravel's disk system and may get wiped during deployments. - Trusting the client's file name. Let Laravel generate names, or sanitise them, to avoid overwriting files and path problems.
- Weak validation. Always validate type and size. For uploads users can view, restrict types to what you actually expect.
- Forgetting the link on a new server. Run
storage:linkas part of deployment, or images break after a fresh install. - Not deleting old files. When a user replaces a photo, remove the old one with
Storage::disk('public')->delete($oldPath). - Storing private files on the public disk because it is easier to display them; anything sensitive belongs on a private disk behind authorization.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.