0

I want to upload a image to a sub directory (public/uploads). after submit it returns successful but it doesn't save the image

public function storeMedia(Request $request)
{
 $file = $request->file('productImage');
 $name = trim($file->getClientOriginalName());
 $folder = uniqid() . '_' . now()->timestamp;
 $file->storeAs('uploads/'.$folder, $name, ['disk' => 'public']);

 return $folder;
}

it returns 60dc27eb0eb92_1625040875 which is want I need but I can't find the uploaded file

9
  • you have folder public/uploads/$folder ? Commented Jun 30, 2021 at 8:24
  • I believe it has stored it in the Storage folder. Commented Jun 30, 2021 at 8:24
  • @workservice thanks, I been able to locate it but I it stored in public/uploads not Storage folder Commented Jun 30, 2021 at 8:34
  • @АлександрЧерножуков yes I have a public/uploads folder Commented Jun 30, 2021 at 8:35
  • You specified that it should be in the public disc, so it will be in /storage/app/public/uploads. Commented Jun 30, 2021 at 8:35

2 Answers 2

3

By default laravel stores file in storage folder. If you want your image file need to be accessible publicly then you need to create symbolic link.

refer this Laravel public disk

  1. put the folder link path in config/filesystems.php

public_path('storage/uploads') => storage_path('app/public/uploads')

  1. Run artisan command to create symbolic link

php artisan storage:link

Here you can refer my another detailed answer for symbolic link uploading file publicly accessible

Sign up to request clarification or add additional context in comments.

Comments

-2

On your config/filesystems.php file, search for public_uploads and update the root to public_path() . '/uploads' and that must do the trick

3 Comments

@Daniel Mordi remember this is not a good approach. This way all your files will be directly uploaded in public folder and it will have security flaws if you dont want all files to publicly accessible.
There is a way to bypass this and put them in Storage as well
@workservice And why would you do it that way, instead of just using the native public disc with Laravel? Storing them outside public, and using a symlink is the best and standard practice for a reason..

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.