Laravel本地环境127.0.0.1:8000无法加载cPanel上传图片的问题
问题:cPanel上传的图片在Laravel本地服务器(127.0.0.1:8000)无法加载,返回404
- 在cPanel上传的图片,运行
php artisan serve后,在127.0.0.1:8000地址下无法加载,返回404错误 - 已尝试操作:
- 执行
php artisan storage:link,提示链接已存在 - 调整
filesystem.php配置 - 检查文件权限
- 执行
- 问题仍未解决,求排查遗漏步骤
相关配置代码
filesystem.php
<?php return [ 'default' => 'local', 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), 'throw' => false, ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => 'http://127.0.0.1:8000/storage', 'visibility' => 'public', 'throw' => false, ], 'custom' => [ 'driver' => 'local', 'root' => public_path('storage'), 'url' => 'http://127.0.0.1:8000/public/storage', 'visibility' => 'public', 'throw' => false, ], 'chat' => [ 'driver' => 'local', 'root' => public_path('storage/chat'), 'url' => 'http://127.0.0.1:8000/public/storage/chat', 'visibility' => 'public', 'throw' => false, ], 'wasabi' => [ 'driver' => 'wasabi', 'key' => env('WASABI_KEY'), 'secret' => env('WASABI_SECRET'), 'region' => env('WASABI_REGION'), 'bucket' => env('WASABI_BUCKET'), ], 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), 'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false), 'throw' => false, ] ], 'links' => [ public_path('storage') => storage_path('app/public'), ], ];
app.php
/* |-------------------------------------------------------------------------- | Application URL |-------------------------------------------------------------------------- | | This URL is used by the console to properly generate URLs when using | the Artisan command line tool. You should set this to the root of | your application so that it is used when running Artisan tasks. | */ 'url' => 'http://127.0.0.1:8000', 'asset_url' => 'http://127.0.0.1:8000/public',
.env
APP_DEBUG=true APP_URL=http://127.0.0.1:8000/
命令执行结果
执行php artisan storage:link返回:
ERROR The [C:\Users\..\..\project\public\storage] link already exists.
(翻译:错误:[C:\Users....\project\public\storage] 链接已存在。)
排查步骤及解决方案
重新创建存储软链接
提示链接已存在但可能无效,先删除现有链接再重建:# Windows系统手动删除public/storage文件夹,或执行 rmdir public\storage # 重新创建链接 php artisan storage:link修正
asset_url配置app.php中的asset_url指向public目录错误,Laravel的public本身就是Web根目录,修改为:'asset_url' => env('APP_URL'),同时将
.env中的APP_URL末尾斜杠去掉:APP_URL=http://127.0.0.1:8000确认图片存放路径
cPanel上传的图片必须放在storage/app/public目录下,public/storage是软链接,直接上传到这里的图片不会被正确映射,需要移到storage/app/public对应路径下。使用正确的URL生成方式
不要手动拼接图片URL,用Laravel内置方法生成:// 方式1 asset('storage/your-image.jpg') // 方式2 Storage::disk('public')->url('your-image.jpg')清理配置缓存
执行命令刷新配置缓存:php artisan config:clear php artisan cache:clear
内容的提问来源于stack exchange,提问作者Emre Sural
相关产品推荐
相关产品推荐

