dave.howson's avatar

Uploading an image via a form

How do I upload an image via a form? Answers on stackoverflow tell me that I should use an approach as

$photoName = time().'.'.$request->user_photo->getClientOriginalExtension();
 $request->user_photo->move(public_path('avatars'), $photoName);

But after reading the documentation about file storage, I decided to go with the approach

 $path = $request->file('avatar')->store('avatars');

This way the path of the image is /avatars

However, when I use asset('storage/'.$user->path) to echo the url in order to display this image within html, my link points to the public directory storage/app/public but the image is stored in storage/app/avatars.

Is the approach I used correct or should I go with the stackoverflow way?

If the approach I used in correct, how do I correctly link the web page to the image?

0 likes
4 replies
mustafaabdujalil's avatar
Level 2

The doc way is the best and easier way to store and get the file from public

to store $path = $request->file('here name of input of file')->store('file name in public directory');

to get file in view {{asset('$path')}}

1 like
dave.howson's avatar

@mustafaabdujalil I tried but there seems to be an issue with the links. $path = $request->file('avatar')->store('avatars'); stores the file in storage/app/avatars/ but {{asset('$path')}} links to localhost:8000/avatars/

dave.howson's avatar

Does anyone know what I can do? I created a symbolic link as per documentation. This links public/storage to storage/app/public. The issue is $path = $request->file('avatar')->store('avatars'); stores images inside storage/app/avatars and so the view cannot access this folder because it is not linked by the symbolic link.

Snapey's avatar
$path = $request->file('avatar')->store('public/avatars');

and then it should appear to be in public/storage/avatars

1 like

Please or to participate in this conversation.