求助:商品图片无法正常显示问题排查(附代码及目录结构)
Let's break down what's going wrong and work through fixes step by step—this is a common snag with Laravel's storage setup, so we'll get your images showing up in no time!
1. Fix the Path Mismatch First
Looking at your code:
<img src="/storage/2020/08/18{{ $product['image'] }}" />
But your actual image lives at public/storage/2020/mySampleImage.png. The extra /08/18 in your src path is pointing to a subdirectory that doesn't exist. You have two quick fixes here:
- Remove the hardcoded
/08/18segment so your path becomes/storage/2020/{{ $product['image'] }}, or - Double-check that
$product['image']includes the correct subfolder structure (but from your example, it looks like it's just the filename, so the first option is likely right).
2. Verify the Laravel Storage Symbolic Link
Laravel relies on a symbolic link between public/storage and storage/app/public to make storage files accessible via the web. If you haven't created this link yet, run this command in your project root:
php artisan storage:link
If you already ran it, confirm the link exists—sometimes it breaks during deployments or file moves. On Linux/macOS, you can check with ls -la public/ to see if storage points to ../storage/app/public.
3. Check What $product['image'] Actually Returns
Make sure the variable is pulling the correct filename (mySampleImage.png in your case). Add a quick debug line in your template to confirm:
{{ dd($product['image']) }}
If it returns something like 08/18/mySampleImage.png, you'll need to adjust either your template path or how you're storing the image path in your database.
4. Fix File Permissions
Ensure your storage directory and files have the right permissions so the web server can access them. Run these commands (adjust the user/group if your server uses something other than www-data):
chmod -R 755 storage/ chown -R www-data:www-data storage/
5. Test the Direct Image URL
Finally, try accessing the image directly in your browser:
http://your-domain.com/storage/2020/mySampleImage.png
If this loads, the problem is definitely in your template path. If it doesn't, go back and recheck the symbolic link and permissions.
内容的提问来源于stack exchange,提问作者user11814855

