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
- Incorrect filename path in
move(): You're passing a full file path as the "filename" parameter tomove(), which isn't how the method works.move($destinationPath, $filename)expects$filenameto be just the filename (not the full directory path). - Storing a
SplFileInfoobject in the database: The$uploadedvariable returned bymove()is aSplFileInfoinstance, not a string path. Your database'simagefield expects a string, which triggers thegetSize()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/postsdirectory 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_sizeinphp.iniis larger thanupload_max_filesize(this controls the total size of all form data, including files)- Restart Apache after any
php.inichanges 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
相关产品推荐
相关产品推荐

