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

Laravel 6图片上传报错:RuntimeException SplFileInfo::getSize()异常

Fix for "SplFileInfo::getSize(): stat failed" and Missing Database Record in Laravel 6 Image Upload

Let's break down your issue and fix it step by step. The error you're seeing and the missing database record are both tied to incorrect file handling in your controller code—even though the file is reaching the target directory, the downstream logic is broken.

Key Issues in Your Code

  1. Incorrect filename path in move(): You're passing a full file path as the "filename" parameter to move(), which isn't how the method works. move($destinationPath, $filename) expects $filename to be just the filename (not the full directory path).
  2. Storing a SplFileInfo object in the database: The $uploaded variable returned by move() is a SplFileInfo instance, not a string path. Your database's image field expects a string, which triggers the getSize() error when Laravel tries to serialize the object.

Step-by-Step Fixes

1. Correct the File Upload Logic

Update your controller's store method to properly handle the filename and path:

public function store(Request $request) {
    $data = $this->validate($request, [
        "title" => "required|string",
        "content" => "required|string",
        "category_id" => "required",
        "image" => "required|image"
    ]);

    $file = $request->image;
    // Generate only the filename (no full path)
    $filename = time() . '.' . $file->getClientOriginalExtension();
    // Define the destination directory (ensure this folder exists in public/)
    $destinationPath = public_path('uploads/posts');
    // Move the file to the target directory
    $file->move($destinationPath, $filename);
    // Create the relative path to store in the database
    $imagePath = 'uploads/posts/' . $filename;

    $post = Post::create([
        "title" => $request->title,
        "content" => $request->content,
        "category_id" => $request->category_id,
        "image" => $imagePath // Store the string path, not the SplFileInfo object
    ]);

    // Optional: Add a redirect with success message
    return redirect()->route('post.index')->with('success', 'Post created successfully');
}

2. Verify Directory Permissions

Even though the file is uploading, double-check:

  • The public/uploads/posts directory exists (create it manually if it doesn't)
  • XAMPP's temporary directory (C:\xampp\tmp\) has read/write permissions for the Apache service user (on Windows, ensure the user running Apache can access this folder)

3. Additional PHP Configuration Checks

You confirmed upload_max_filesize is consistent, but also verify:

  • post_max_size in php.ini is larger than upload_max_filesize (this controls the total size of all form data, including files)
  • Restart Apache after any php.ini changes to apply them

Bonus: Use Laravel Storage for Better File Management

For a more robust solution, use Laravel's built-in Storage facade—it handles paths, permissions, and cloud storage seamlessly:

// First run `php artisan storage:link` to create a symlink from public/storage to storage/app/public
$imagePath = $request->file('image')->store('posts', 'public');
// Store the path in the database (e.g., 'posts/abc123.jpg')
Post::create([
    // ... other fields
    "image" => $imagePath
]);
// To display the image later: asset('storage/' . $post->image)

内容的提问来源于stack exchange,提问作者Ahmed Malek

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:05:29