admin管理员组文章数量:1321239
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
|
1 Answer
Reset to default 1check if input name have [] in the last like this
<input type="file" name="images[]" id="images" multiple>
本文标签: phpMultiple images upload not working in laravel 11Stack Overflow
版权声明:本文标题:php - Multiple images upload not working in laravel 11 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742097800a2420657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
count($req->file('images'))
? – C3roe Commented Jan 17 at 15:08name="images[]"
, if you want to use multiple file input fields, or one field with themultiple
attribute set. Without the square brackets, PHP will overwrite multiple parameters with the same name. – C3roe Commented Jan 17 at 15:12dd($req)
? If they are really not in the request it must be the frontend. – JorisJ1 Commented Jan 19 at 20:04