objectiveshark's avatar

Can't access an uploaded file (404)

I have this code inside my product controller

$product->image = $request->file('image')->store('products', 'public');

and inside my blade template I have this code

{{ asset($product->image) }} which returns this http://laravel6.test/products/P6kGcLTf8eoxSQZcBSAJf79SNdLqggxhsNxjM62Z.jpeg

I had executed php artisan storage:link and see the uploaded file with file explorer. I tried prepending storage to the url like so http://laravel6.test/storage/products/P6kGcLTf8eoxSQZcBSAJf79SNdLqggxhsNxjM62Z.jpeg still get 404. Please, help.

0 likes
11 replies
rameezisrar's avatar

@objectiveshark if you have file stored under the root/storage/app/public/[folder_name]/[file_name]

then it should be accessible as

asset('storage/images/'.$article->image)'

or

<img src="{{ url('storage/[folder_name]/'.$object->image) }}" alt="" title="" />

if you have file getting stored under the root/public/[folder_name]/[file_name] then it should be accessible as

http://yout.site.link/[folder_name]/[file_name]
Snapey's avatar

Is your site hosted properly so that the index.php is at the root and not in some sub-folder.

ie, do you see public in your URLs ?

objectiveshark's avatar

@snapey it's default laravel installation that is hosted on local homestead environment. My index.php is in public folder and I don't see public in my URLs. Btw, I can access css and js files which are located inside public/css & public/js folders just fine. E.g. http://laravel6.test/css/app.css is accessible.

Snapey's avatar

So if you look in public/storage/images folder, do you see your file? If not, what folder is it in?

Snapey's avatar

So if you construct the image tag like

<img src="/storage/{{ $product->image }}" />

?

Snapey's avatar

What are your other URLs like?

Do you have a standard .htaccess file?

Are you working in a case sensitive environment?

objectiveshark's avatar
objectiveshark
OP
Best Answer
Level 1

So it solved by itself. I just deleted the old symlink and regenerated it with php artisan storage:link and reloaded the homestead instance. I think I might know the culprit of this, the first time I used php artisan storage:linkwithin homestead ssh.

P.S. Thank you for everyone who responded.

1 like
dbestech's avatar

If you have this problem, you may delete public/storage folder and the run php artisan storage:link works. that command will generate public/storage folder you.

1 like
fredpedersen's avatar

To add to this in case anyone else has this issue. For me I needed to delete the symlink and run ./vendor/bin/sail php artisan storage:link

php artisan storage:link alone causes the error.

Please or to participate in this conversation.