Latheesan
Latheesan

Reputation: 24116

Saving uploaded image file in public dir not working in laravel 5.6

I have created a designated location to store all the uploaded images in public dir like this:

enter image description here

and I have the default config/filesystem.php for public driver like this:

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

Within my repository, I am trying to save the uploaded image like this:

    // ... snipped        

    if (array_key_exists('logo', $validated)) {
        $logoPath = $validated['logo']->store(
            'uploads/logos',
            'public'
        );
        $company->logo = url($logoPath);
    }

    $company->save();

In the database, I can see the value of logo field for the company record like this: http://my-app.local/uploads/logos/3hlsAhnnIPjd4zhdpfhvKw4tqDkpcCz23NczwhVM.png

However, the public/uploads/logos dir is empty. Any idea what might be wrong here? Am I supposed to use the ->move() method on the UploadedFile instead of ->store()?

Upvotes: 1

Views: 1253

Answers (2)

Giedrius Kiršys
Giedrius Kiršys

Reputation: 5324

Actually, yes, it was your mistake, but not the one you just found. By default laravel local storage points to storage/app/public directory, because laravel wants you to act professionally and store all uploaded files in storage, not in some kind of public directory. But then you would ask - how on earth user should access non-public storage directly? And there is a caught - php artisan storage:link command. It creates symbolic link from public directory to storage directory (storage/app/public -> public/storage). This is mostly because of deployments and version management.

Upvotes: 0

aj3sh
aj3sh

Reputation: 384

You can use an alternative method for uploading images in your public directory.

$request->validate([
    'logo' => 'image|required',
]);

if($request->hasFile('logo')){
   $company->logo = $request->image->move('uploads/logo', $request->logo->hashName());
}
$company->save()

Upvotes: 1

Related Questions