save image in public folder instead storage laravel 5

save image in public folder instead storage laravel 5

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why storage_path('image') Doesn't Put Files in Your Web Root

You want to save an avatar so it can be served directly by your web server, but you're currently landing up in storage/app. The instinct to edit config/filesystem.php and point the 'public' disk to a custom directory like storage_path('image') is understandable, but it addresses the wrong question.

In Laravel, the filesystem package manages where files are stored on the disk (the root path), not how they are served by the web server. By changing the configuration to point to a folder outside your public directory (or inside storage which isn't typically web-accessible without specific aliasing), you haven't actually made the file reachable via HTTP from your browser's perspective in the way you expect.

The core issue lies in how your View renders the image and where the actual files are being persisted. Let's break down the architecture and fix the implementation so you can serve images directly from public.

The Misconception About Disks

When you update the disks array in config/filesystem.php, you are defining logical paths for Laravel's Storage facade.

'disks' => [
    'public' => [
        'driver' => 'local',
        'root' => storage_path('image'), // This points to storage/image, not public/
        'url' => env('APP_URL').'/public',
        'visibility' => 'public',
    ],
],

This configuration tells the Storage facade that when you call Storage::disk('public')->put(), it should write to storage/image. However, your web server (Apache or Nginx) is likely configured to serve requests only from the public directory. If your file sits in storage/image, your web server will throw a 403 Forbidden error because that path is not publicly accessible via HTTP.

You cannot simply reconfigure the disk root to bypass the physical file system locations; you must place files where the web server expects them or configure the server to alias that location.

The Correct Approach: Use public_path

To save an image in a folder like public/images so it is immediately accessible via a URL, you need to change your storage root logic or use the facade explicitly pointing to the public directory.

Instead of relying on the default 'public' disk configuration which defaults to storage/app/public, you should configure your upload logic to target public_path().

Here is how you should adjust your Controller to save directly into the web-accessible folder:

use Illuminate\Support\Facades\Storage;

public function save(Request $request)
{
    // Validate the file first
    $request->validate([
        'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);

    // Determine the destination directory inside public
    $directory = 'images'; 

    // Store the file. 
    // If you want absolute control over the path, use saveAs or put
    // If using store(), it creates a hash-based filename by default.
    // To keep original name in public/images:
    
    $path = Storage::disk('public')->putFileAs(
        $directory, 
        $request->file('image'), 
        'avatar_' . time() . '.' . $request->file('image')->getClientOriginalExtension()
    );

    // Note: The returned path is relative (e.g., 'images/avatar_123.jpg')
    
    DB::table('pictbl')->insert([
        'original_name' => $request->file('image')->getClientOriginalName(),
        'format' => $request->file('image')->getClientOriginalExtension(),
        'path' => $path, // Store relative path, not full filesystem path
    ]);

    return view('pic.pic', compact('path'));
}

Updating the View and Routes

Your current View renders the image using storage/app/..., which is incorrect for public access unless you have a specific .htaccess or Nginx rewrite rule. You need to reference the public URL directly.

Assuming your web root serves public, the URL for an image in public/images/avatar.jpg would be /images/avatar.jpg.

Update your View:

{!! Form::open(['url' => 'saved', 'method' => 'post', 'files' => true]) !!}
    {!! Form::file('image') !!}
    {!! Form::submit('save') !!}
{!! Form::close() !!}

<!-- Render the image using the relative path stored in the database -->
<img src="{{ asset('images/' . $path) }}" alt="Avatar">

And update your Route to ensure it hits the correct controller method:

Route::post('/saved', 'Test2Controller@save');
Route::get('/pic', function () {
    return view('pic.pic');
});

Why This Works

By using Storage::disk('public')->putFileAs() with the directory set to 'images', Laravel writes the file to storage/app/public/images. The magic happens because Laravel's default public disk configuration points to storage/app/public. You then serve this content via your public folder.

If you strictly require files to live in a folder named exactly public (which is generally discouraged for security reasons regarding symlink attacks), you would need to modify the root of your local disk in config/filesystem.php to point directly to public_path():

// In config/filesystems.php - Only do this if you accept the security implications
'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => public_path(), // Points straight to web root
        'visibility' => 'public',
    ],
],

However, sticking to storage/app/public and serving it via asset() is the standard, secure, and recommended Laravel pattern. It keeps uploaded files separate from your application source code while still being accessible to users.

Our network