Last active
January 8, 2025 20:01
-
-
Save EduardoSP6/bb21432088ff9459f0bcc43026684763 to your computer and use it in GitHub Desktop.
Download file from Amazon S3 with PHP + Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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