I am trying to loop over a multiple input file select element:
<label class="btn btn-success btn-md rounded-1" for="galleryFile">Create Album</label>
<input type="file" name="galleryFile[]" id="galleryFile" class="d-none" multiple accept="image/*">
and save all selected images to AWS s3:
public function sendToCloud($file, $folder, $prefix) {
$filePath = Auth::user()->userid . '/' .$folder.'/' . $prefix;
$extension = $file->getClientOriginalExtension();
$filename = $filePath . time() . '.' . $extension;
Storage::disk('s3')->put($filename, fopen($file, 'r+'), 'public');
return Storage::disk('s3')->url($filename);
}
$unikPath = uniqid();
foreach ($request->file('galleryFile') as $key => $file) {
if ($key == array_key_last($request->file('galleryFile'))) {
$galleryUrl .= $this->sendToCloud($file, $unikPath . '/gallery', 'img_');
} else {
$galleryUrl .= $this->sendToCloud($file, $unikPath . '/gallery', 'img_') . ' | ';
}
}
I am attempting to loop over a multiple input file select element and save all the selected images to AWS s3.
When testing locally, it works as expected, but when deployed to Heroku, it randomly picks two of the images and saves them to S3, and returns the S3 url of the two images multiple times.
I am seeking assistance in resolving this issue.
<label class="btn btn-success btn-md rounded-1" for="galleryFile">Create Album</label>
<input type="file" name="galleryFile[]" id="galleryFile" class="d-none" multiple accept="image/*">
public function sendToCloud($file, $folder, $prefix) {
$filePath = Auth::user()->userid . '/' .$folder.'/' . $prefix;
$extension = $file->getClientOriginalExtension();
$filename = $filePath . time() . '.' . $extension;
Storage::disk('s3')->put($filename, fopen($file, 'r+'), 'public');
return Storage::disk('s3')->url($filename);
}
$unikPath = uniqid();
foreach ($request->file('galleryFile') as $key => $file) {
if ($key == array_key_last($request->file('galleryFile'))) {
$galleryUrl .= $this->sendToCloud($file, $unikPath . '/gallery', 'img_');
} else {
$galleryUrl .= $this->sendToCloud($file, $unikPath . '/gallery', 'img_') . ' | ';
}
}