Laravel 5.6如何通过一个方法同时上传文件到两个文件夹?
Hey there! Let's break down why your thumb upload is failing and get it sorted out quickly.
The Root Cause
That error Unable to find file (/tmp/phpUSxbEJ) happens because when you upload a file in Laravel, it first gets stored in a temporary location (like /tmp/...). When you use methods like store() or move() to save it to the product folder, Laravel moves the temporary file to its new home—so the original temp file no longer exists. When you try to reuse the same file instance to upload to thumbs, it's looking for a temp file that's already gone!
Solution 1: Use Intervention Image to Handle Both Saves in One Go
Since you're already using Intervention Image, the cleanest fix is to load the uploaded file into an Image instance first. This keeps the image data in memory, so you can save it to both folders without worrying about the temp file disappearing.
Here's a working code example:
use Intervention\Image\Facades\Image; use Illuminate\Http\Request; public function uploadProductImage(Request $request) { // Validate the uploaded file first $request->validate([ 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', ]); if ($request->hasFile('image')) { $uploadedImage = $request->file('image'); // Generate a unique filename to avoid overwrites $filename = uniqid() . '.' . $uploadedImage->getClientOriginalExtension(); // Load the uploaded file into an Intervention Image instance $image = Image::make($uploadedImage->getRealPath()); // Save the full-size image to the product folder $image->save(public_path('product/' . $filename)); // Resize to create a thumbnail (adjust dimensions as needed) $image->resize(300, null, function ($constraint) { $constraint->aspectRatio(); // Keep the image's original aspect ratio $constraint->upsize(); // Don't stretch small images larger }); // Save the thumbnail to the thumbs folder $image->save(public_path('thumbs/' . $filename)); return redirect()->back()->with('success', 'Image and thumbnail uploaded successfully!'); } return redirect()->back()->with('error', 'No image file was uploaded.'); }
Solution 2: Save the Original First, Then Generate Thumbnail from It
If you prefer to first save the original file to product, then generate the thumbnail by reading that saved file (instead of relying on the temp file), this approach works too:
use Intervention\Image\Facades\Image; use Illuminate\Http\Request; public function uploadProductImage(Request $request) { $request->validate([ 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', ]); if ($request->hasFile('image')) { $uploadedImage = $request->file('image'); $filename = uniqid() . '.' . $uploadedImage->getClientOriginalExtension(); // Save the original image to product folder first $originalPath = public_path('product/' . $filename); $uploadedImage->move(public_path('product'), $filename); // Load the saved original image to create the thumbnail $thumbnail = Image::make($originalPath); $thumbnail->resize(300, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); // Save the thumbnail to thumbs folder $thumbnail->save(public_path('thumbs/' . $filename)); return redirect()->back()->with('success', 'Images uploaded successfully!'); } return redirect()->back()->with('error', 'Upload failed: no file provided.'); }
Important Checks
- Folder Permissions: Make sure both
public/productandpublic/thumbsfolders have write permissions. On most servers, you can run this command (adjust paths if needed):chmod -R 755 public/product public/thumbs - Intervention Setup: Double-check that Intervention Image is properly installed and configured in your Laravel 5.6 project (you should have the service provider and facade set up in
config/app.php).
内容的提问来源于stack exchange,提问作者moses toh

