Das Hochladen mehrerer Bilder funktioniert in Laravel 11 nichtPhp

PHP-Programmierer chatten hier
Guest
 Das Hochladen mehrerer Bilder funktioniert in Laravel 11 nicht

Post by Guest »

Ich habe versucht, eine foreach-Schleife zu verwenden, um die Anforderungsdateien zu durchlaufen, aber nur das letzte Bild in der Schleife wird in der Datenbank und im Dateisystem gespeichert. Wie kann ich das beheben? Ich wäre für jede Hilfe dankbar,
hier ist mein Code

Code: Select all

$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);
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post