<?php | |
/************************************************************************* | |
* Get File Information | |
*/ | |
// Assuming these come from some data source in your app | |
$s3FileKey = 's3/key/path/to/file.ext'; | |
$fileName = 'file.ext'; | |
// Create temporary download link and redirect | |
$adapter = Storage::disk('s3')->getAdapter(); | |
$client = $adapter->getClient(); | |
$client->registerStreamWrapper(); | |
$object = $client->headObject([ | |
'Bucket' => $adapter->getBucket(), | |
'Key' => /*$adapter->getPathPrefix() . */$s3FileKey, | |
]); | |
/************************************************************************* | |
* Set headers to allow browser to force a download | |
*/ | |
header('Last-Modified: '.$object['LastModified']); | |
// header('Etag: '.$object['ETag']); # We are not implementing validation caching here, but we could! | |
header('Accept-Ranges: '.$object['AcceptRanges']); | |
header('Content-Length: '.$object['ContentLength']); | |
header('Content-Type: '.$object['ContentType']); | |
header('Content-Disposition: attachment; filename='.$fileName); | |
/************************************************************************* | |
* Stream file to the browser | |
*/ | |
// Open a stream in read-only mode | |
if (!($stream = fopen("s3://{$adapter->getBucket()}/{$s3FileKey}", 'r'))) { | |
throw new \Exception('Could not open stream for reading file: ['.$s3FileKey.']'); | |
} | |
// Check if the stream has more data to read | |
while (!feof($stream)) { | |
// Read 1024 bytes from the stream | |
echo fread($stream, 1024); | |
} | |
// Be sure to close the stream resource when you're done with it | |
fclose($stream); |
This comment has been minimized.
This comment has been minimized.
Hey @fideloper, I bought your Scaling Laravel course and slowly but surely implementing all that good stuff into my app! I've just implemented your |
This comment has been minimized.
This comment has been minimized.
hey @fideloper . There's an extra paranthesis at the end of
causing a syntax error Fideloper Update: Fixed that, thanks! |
This comment has been minimized.
This comment has been minimized.
@fideloper thanks so much, saved me hours. |
This comment has been minimized.
This comment has been minimized.
@fideloper thanks for this |
This comment has been minimized.
This comment has been minimized.
this is such a good script, works with Lumen (https://gist.github.com/digitalkreativ/17cd94db914e6cb21b5dd9e675dd9abe) , while S3FileStream.php does not (many classes missing) tnx for sharing |
This comment has been minimized.
This comment has been minimized.
Why is this being downloaded for me instead of being displayed? |
This comment has been minimized.
This comment has been minimized.
@lovecoding-git comment this line: header('Content-Disposition: attachment; filename='.$fileName); This header tells the browser to force download instead of just displaying when the it knows the content type. |
This comment has been minimized.
This comment has been minimized.
Why not to use |
This comment has been minimized.
Reference:
Docs on HeadObject S3 API call:
http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#headobject
Example streaming file and explanation on "$client->registerStreamWrapper()" method:
https://aws.amazon.com/blogs/developer/amazon-s3-php-stream-wrapper/
Force file download via
Content-Disposition
header:https://stackoverflow.com/questions/8485886/force-file-download-with-php-using-header