16:43:54 22/6/2025 - 0 lượt xem

All the Ways to Store Images in Laravel

When working with image uploads in Laravel, there are multiple approaches to store files depending on how much control you want over the filename, storage location, and processing. Below are the different ways to store images in Laravel.

Note: All examples below assume you're storing files in the storage/app/public directory for easier access via symbolic linking (php artisan storage:link).

​For example, in frontend will send a file:

<template    >	
	<input    
	ref="fileInput"
	type="file"
	accept=".diff,.patch"
	@change="handleFileSelect"
	class="d-none"
	/>
<template    >                            
<script     setup lang="ts">
const handleFileSelect = (event: Event): void => {
    const target = event.target as HTMLInputElement;
    const file = target.files?.[0];
    if (file) {
        uploadFile(file);
    }
};
const uploadFile = async (file: File): Promise<void   > => {
    try {
        const formData = new FormData();
        formData.append('file', file);
		// xfetch is my custom function to send request
        const response = await xfetch({
            url: '/upload',
            method: 'POST',
            data: formData
        });
    } catch (error) {
        console.error('Upload failed:', error);
    }
};
 </script    >


✅ Way 1: Basic store method

public function upload(Request $request)
{
    $path = $request->file('file')->store('products', 'public');
}

Laravel will automatically generate a unique filename for you. This is a quick and easy method when filename customization isn't needed.


✅ Way 2: Store with custom filename using storeAs()

public function upload(Request $request)
{
    $filename = time() . '.' . $request->file('file')->getClientOriginalExtension();
    $path = $request->file('file')->storeAs('products', $filename, 'public');
}

Use this method when you want full control over the filename. You can use getClientOriginalName() or generate a name manually.


✅ Way 3: Store using putFile()

public function upload(Request $request)
{
    $path = Storage::disk('public')->putFile('products', $request->file('file'));
}

This is similar to the store() method, but it uses the Storage facade and is a bit more flexible if you want to work with different disks dynamically.


✅ Way 4: Store using putFileAs() for custom filename

public function upload(Request $request)
{
    $filename = 'image_' . time() . '.' . $request->file('file')->getClientOriginalExtension();
    $path = Storage::disk('public')->putFileAs('products', $request->file('file'), $filename);
}

This is the most customizable option using the Storage facade with custom filenames and specified disk.


✅ Way 5: Move the file manually

public function upload(Request $request)
{
    $file = $request->file('file');
    $filename = 'manual_' . time() . '.' . $file->getClientOriginalExtension();
    $file->move(public_path('storage/products'), $filename);
}

This is not the preferred Laravel way, but it's closer to native PHP. Only use this if you need to place files directly in the public/ directory.


🛠 Issue in Laravel: File only stores in public folder, not storage

Sometimes, when you store a file using Laravel's methods, it may seem like it's only stored in the public folder and not in the storage directory. This usually happens due to cached configuration.

✅ I fixed it with the command:

php artisan config:clear

This command clears Laravel's cached configuration and allows your app to correctly use the storage paths as expected.