1

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()?

4
  • Is it maybe in the storage folder (storage/app/public by any chance)? Commented Apr 22, 2018 at 0:23
  • ah! you're right 'root' => storage_path('app/public'), I think i need to update this path. Let me check. Commented Apr 22, 2018 at 0:24
  • Yup, it's been a minute since I worked with file upload. But storage_path() helper will generate the path to the storage folder. You probably just want to change that to something like public_path('uploads'). Or create a symlink from storage folder to public folder Commented Apr 22, 2018 at 0:26
  • Yea, this was the problem, i didn't pay enough attention to the filesystem definition. Silly mistake. Commented Apr 22, 2018 at 0:31

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.