#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

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.

LocationPublicly Accessible?Typical Use
storage/appNo, by defaultPrivate documents, internal files
storage/app/publicYes, via symbolic linkProfile photos, product images
public/storageYes, directlyThe publicly served link to the folder above

Setting Up the Public Link

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 public folder. 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:link as 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.

Explore Course