You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行php artisan storage:link后storage未关联public,网站头像无法显示

1. 手动创建软链接

命令行自动创建失败时,直接手动执行对应系统的命令(需在项目根目录操作):

  • Linux/macOS:
    先删除可能存在的无效链接:
    rm public/storage
    
    再创建新链接:
    ln -s ../storage/app/public public/storage
    
  • Windows:
    命令提示符(需管理员权限):
    mklink /d public\storage ..\storage\app\public
    
    PowerShell(需管理员权限):
    New-Item -ItemType SymbolicLink -Path "public/storage" -Target "storage/app/public"
    

2. 检查目录权限

确保storage/app/public和public目录的权限足够让Web服务器读写:

chmod -R 755 storage public
# 若需要,调整目录所属用户(以www-data为例)
chown -R www-data:www-data storage public

3. 验证文件系统配置

打开config/filesystems.php,确认public磁盘的配置正确:

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

同时确保.env文件中的APP_URL设置正确(比如APP_URL=http://your-domain.com),之后清除配置缓存:

php artisan config:clear

4. 确认头像URL生成逻辑

确保代码中生成头像链接时使用Laravel提供的方法,避免硬编码路径:

  • 使用asset()辅助函数:
    asset('storage/avatars/'.$user->avatar)
    
  • 或使用Storage门面的url()方法:
    Storage::disk('public')->url($user->avatar)
    

内容的提问来源于stack exchange,提问作者Alfin Faiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:04:55