Skip to content

Instantly share code, notes, and snippets.

@EduardoSP6
Last active January 8, 2025 20:01
Show Gist options
  • Save EduardoSP6/bb21432088ff9459f0bcc43026684763 to your computer and use it in GitHub Desktop.
Save EduardoSP6/bb21432088ff9459f0bcc43026684763 to your computer and use it in GitHub Desktop.
Download file from Amazon S3 with PHP + Laravel
<?php
public function download($archive)
{
$s3Client = Storage::disk('s3');
$fileName = File::basename($archive->url);
$fullPath = env('AWS_PATH') . "imports/" . $fileName;
if ($s3Client->exists($fullPath)) {
$headers = [
'Content-Type' => $s3Client->getMimetype($fullPath),
'Content-Length' => $s3Client->getSize($fullPath),
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment; filename=". $fileName,
];
return \Response::make($s3Client->get($fullPath), 200, $headers);
} else {
return redirect()->back()->with("error", "Arquivo não encontrado!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment