最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Multiple images upload not working in laravel 11 - Stack Overflow

programmeradmin5浏览0评论

I tried using a foreach loop to iterate through the request files, but only the last image in the loop is saved to the database and file system. How can I fix this? any help would be appreciated, here is my code

$galleryArr = []; 
$allowedExtensions = ['jpg', 'png', 'jpeg']; 
$counter = 1;

if ($req->hasFile('images')) {
  foreach ($req->file('images') as $file) {
    $fileExtension = $file->getClientOriginalExtension();
    $fileName = $currentTimestamp . "_" . $counter . "." . $fileExtension;
    // Handle thumbnail creation
    $this->generateProductThumbnailImage($file, $fileName);
    // Add to gallery array
    $galleryArr[] = $fileName;
    $counter++;
  }
}

// Convert gallery array to comma-separated string
$products->images = implode(',', $galleryArr);

// Save the product data
$products->save();

public function GenerateProductThumbnailImage($image, $imageName) {
  $destinationPathThumbnail = public_path('img/prod/thumbnail');
  $destinationPath = public_path('img/prod');
  $img = Image::make($image->path());

  // Save the original image
  $img->resize(540, 689, function($constraint) { $constraint->aspectRatio(); })->save($destinationPath . '/' . $imageName);

  // Save the thumbnail image
  $img->resize(104, 104, function($constraint) { $constraint->aspectRatio(); })->save($destinationPathThumbnail . '/' . $imageName);
}

I tried using a foreach loop to iterate through the request files, but only the last image in the loop is saved to the database and file system. How can I fix this? any help would be appreciated, here is my code

$galleryArr = []; 
$allowedExtensions = ['jpg', 'png', 'jpeg']; 
$counter = 1;

if ($req->hasFile('images')) {
  foreach ($req->file('images') as $file) {
    $fileExtension = $file->getClientOriginalExtension();
    $fileName = $currentTimestamp . "_" . $counter . "." . $fileExtension;
    // Handle thumbnail creation
    $this->generateProductThumbnailImage($file, $fileName);
    // Add to gallery array
    $galleryArr[] = $fileName;
    $counter++;
  }
}

// Convert gallery array to comma-separated string
$products->images = implode(',', $galleryArr);

// Save the product data
$products->save();

public function GenerateProductThumbnailImage($image, $imageName) {
  $destinationPathThumbnail = public_path('img/prod/thumbnail');
  $destinationPath = public_path('img/prod');
  $img = Image::make($image->path());

  // Save the original image
  $img->resize(540, 689, function($constraint) { $constraint->aspectRatio(); })->save($destinationPath . '/' . $imageName);

  // Save the thumbnail image
  $img->resize(104, 104, function($constraint) { $constraint->aspectRatio(); })->save($destinationPathThumbnail . '/' . $imageName);
}
Share Improve this question edited Jan 18 at 19:49 Steve 1,6252 gold badges21 silver badges33 bronze badges asked Jan 17 at 14:59 Nayab AnwarNayab Anwar 11 silver badge1 bronze badge 4
  • Are you sure you have more than one file to loop over? Did you verify this, by checking count($req->file('images'))? – C3roe Commented Jan 17 at 15:08
  • Is / are your form field(s) for the image upload named appropriately? It needs to be name="images[]", if you want to use multiple file input fields, or one field with the multiple attribute set. Without the square brackets, PHP will overwrite multiple parameters with the same name. – C3roe Commented Jan 17 at 15:12
  • @C3roe "I also tried this line: count($req->file('images')), but still only one image is being shown, and the HTML is like this: <input type="file" id="gFile" name="images[]" accept="image/*" multiple>. I don't understand where the issue is. – Nayab Anwar Commented Jan 18 at 10:09
  • Are you using any JS libraries that might interfere with the upload? Also, could you edit your question and add the output of dd($req)? If they are really not in the request it must be the frontend. – JorisJ1 Commented Jan 19 at 20:04
Add a comment  | 

1 Answer 1

Reset to default 1

check if input name have [] in the last like this

  <input type="file" name="images[]" id="images" multiple> 
发布评论

评论列表(0)

  1. 暂无评论