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

Laravel学生存储功能图片无法访问问题求助

Laravel 图片存储后访问404问题排查

问题详情

我实现了一个存储学生数据的接口,图片已成功保存至数据库,但访问图片时返回“Not Found”。此前可正常访问的链接http://localhost:8000/storage/images/6tsncPbIw6PmU91GclYGJWSPLqOB3WjN8JTyrRdO.png现在无法打开,我已执行php artisan storage:link命令,求帮忙排查。对应的store方法代码如下:

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required|email|unique:students',
        'phone_number' => ['required', 'regex:/(^70|^71|^76|^78|^79|^81|^06|^03)[0-9]{6}$/'],
    ]);

    if ($validator->fails()) {
        return response()->json([
            'message' => $validator->errors()->first(),
        ], 422);
    }
    
    $student = new Student; 

    $First_name = $request->input('first_name');
    $Last_name = $request->input('last_name');
    $email = $request->input('email');
    $birth_date = $request->input('birth_date');
    $phone = $request->input('phone_number');
    $enroll = $request->input('enrollment_date');
    //$headshot = $request->input('headshot');
    $image_path = $request->file('image')->store('images', 'public');
    $section_id = $request->input('section_id');
    $request->validate([
        'email' => 'regex:/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix'
    ]);
    $section = Section::find($section_id);

    // 检查section是否存在
    if (!$section) {
        return response()->json([
            'message' => 'No section found to add a new student',
        ], 404);
    }

    // 检查section是否已满员
    $studentCount = $section->student()->count(); 
    if ($studentCount >= $section->capacity) {
        return response()->json([
            'message' => 'The section has reached its capacity',
        ], 400);
    }

    $student->first_name = $First_name;
    $student->last_name = $Last_name;
    $student->email = $email;
    $student->birth_date = $birth_date;
    $student->phone_number = $phone;
    $student->enrollment_date = $enroll;
    // $student->headshot = $headshot;
    $student->headshot = $image_path;
    $student->Section()->associate($section);
    $student->save();


    return  response()->json([
        'success' => true,
        'message' => 'Student was successfully added',
    ]);
}

排查建议

  • 确认物理文件存在性:检查项目根目录下storage/app/public/images/文件夹内是否有对应图片。数据库中headshot字段存储的是相对路径(如images/xxx.png),物理文件必须存在于该路径下。
  • 重新生成软链接:删除项目根目录下的public/storage链接文件,重新执行php artisan storage:link,执行后验证public/storage是否正确指向storage/app/public。
  • 检查目录权限:确保storage/app/public和public/storage目录具备读写权限。Linux环境下可执行chmod -R 755 storage public,或调整目录所属用户为Web服务器运行用户(如www-data)。
  • 验证访问路径匹配:数据库中headshot字段值为images/xxx.png时,正确访问链接应为http://localhost:8000/storage/ + 字段值,确认数据库路径无多余前缀/后缀。
  • 核对文件系统配置:检查config/filesystems.php中public磁盘配置是否正确,默认配置应为:
    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],
    
  • 调试存储逻辑:在$image_path = $request->file('image')->store(...)后添加日志或打印语句,输出$image_path和storage_path('app/public/'.$image_path),确认物理路径是否存在。

内容的提问来源于stack exchange,提问作者Mahmoud Khodor

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 04:16:57