Multiple image upload to s3 via Laravel on Heroku not working

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_') . ' | ';
  }
}

The issue could be related to Heroku’s ephemeral filesystem, which can cause uploaded files to be lost. To solve this, you can try using a cloud-based file storage service like AWS S3 or Google Cloud Storage to store the uploaded files instead of relying on Heroku’s filesystem.

You can also try modifying your code to use a different approach for uploading files, such as chunked uploads or resumable uploads, which can help to ensure that all files are successfully uploaded even if there are issues with the network or server.

Additionally, you may want to check if there are any errors or exceptions being thrown during the file upload process, and if so, try to debug and resolve them.