Change to original file name when Download? - php

I am using amazon S3 service with PHP by using this API
https://github.com/tpyo/amazon-s3-php-class
I am passing the url to client like this
https://domain.s3.amazonaws.com/bucket/filename_11052011111924.zip?AWSAccessKeyId=myaccesskey&Expires=1305311393&Signature=mysignature
So when the client clicks or paste the URL into browser , the file downloaded with the name of filename_11052011111924.zip.But I stored my original filename in DB.
So is it possible to download when passing the URL alone to the client and download with original file name.I am not sure whether this will help me.
Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream

If you set the headers that you listed on your file when you upload it to S3, you will be able to download the file with the original filename. (you can also set these on existing files in S3 - see the AWS docs)
I'm not sure if your library supports this but you can do it with the AWS S3 SDK.
Something like (I don't know php so check the syntax):
// Instantiate the class
$s3 = new AmazonS3();
$response = $s3->create_object('bucket', 'filename_11052011111924.zip', array(
'fileUpload' => 'filename.zip',
'contentType' => 'application/octet-stream',
'headers' => array( // raw headers
'Content-Disposition' => 'attachment; filename=filename.zip',
),
));
Update
You can also adjust certain headers each time you generate a new url. See http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonS3/get_object_url
$url = $s3->get_object_url('bucket', 'filename_11052011111924.zip', '5 minutes', array(
'response' => array(
'Content-Disposition' => 'attachment; filename=filename.zip'
)
));

I don't think that will work (I never tried it though). You might need to download the file to your server first, later use headers, once it is completed (or after sometime later with some bot or cron) you can delete the file(s).
This approach will be using your bandwidth.

Yes, you can tell to AWS how output file must be named:
Note: we encode file name!
$filename = "Here we can have some utf8 chars.ext";
$outputFileName = '=?UTF-8?B?' . base64_encode($filename) . '?=';
$url = $s3->get_object_url(
'bucket_name',
'path_to_the_file.ext',
'5 minutes',
array(
'response' => array(
'content-disposition' => 'attachment;' . " filename=\"" . $outputFileName . "\";")
)
)
);

Related

Zend 2 Multiple access

I build a shop with Zend Framework 2 where you can by digital stuff like music and download them.
Now i have a the following situation:
I have a controller that controlls all downloads. I start to download a large file and it takes several minutes until the download is finished. While i am downloading, i can't browse in my shop with the same browser that I use for the download.
Is it possible to configure zend 2, so that i still can browse my website while downloading or is this impossible due to the fact that i execute the same script by launching the application througb the index.php file?
Update
$response = new Stream();
$response->setStream(fopen($finalFilePath, 'r'));
$response->setStatusCode(200);
$response->setStreamName(basename($finalFilePath));
$headers = new Headers();
$headerArr = array(
'Content-Disposition' => 'attachment; filename="' . basename($finalFilePath) .'"',
'Content-Type' => 'application/octet-stream',
'Content-Transfer-Encoding' => 'Binary',
'Content-Length' => filesize($finalFilePath),
);
$headers->addHeaders($headerArr);
$response->setHeaders($headers);
return $response;
Found a solution. It has something to do with sessions !
just call
session_write_close();
before the "return".

S3 Force Download

I have a website here http://www.voclr.it/acapellas/ my files are hosting on my Amazon S3 Account, but when a visitor goes to download an MP3 from my website it forces them to stream it but what I actually want it to do is download it to there desktop.
I have disabled S3 on the website for now, so the downloads are working fine. but really I want S3 to search the MP3s
Basically, you have to tell S3 to override the content-disposition header of the response. You can do that by appending the response-content-disposition query string parameter to the S3 file url and setting it to the desired content-disposition value. To force download try:
<url>&response-content-disposition="attachment; filename=somefilename"
You can find this in the S3 docs. For information on the values that the content-disposition header can assume you can look here.
As an additional information this also works with Google Cloud Storage.
require_once '../sdk-1.4.2.1/sdk.class.php';
// Instantiate the class
$s3 = new AmazonS3();
// Copy object over itself and modify headers
$response = $s3->copy_object(
array( // Source
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Destination
'bucket' => 'your_bucket',
'filename' => 'Key/To/YourFile'
),
array( // Optional parameters
**'headers' => array(
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment'**
)
)
);
// Success?
var_dump($response->isOK());
Amazon AWS S3 to Force Download Mp3 File instead of Stream It
I created a solution for doing this via CloudFront functions (no php required since it all runs at AWS by linking to the .mp3 file on Cloudfront with a ?title=TitleGoesHere querystring to force a downloaded file with that filename). This is a fairly recent way of doing things (as of August 2022). I documented my function and how I set up my "S3 bucket behind a CloudFront Distribution" here: https://stackoverflow.com/a/73451456/19823883

KnpSnappyBundle Save File To Server

I am missing something in the KnpSnappy Bundle docs. :( How do I save my pdf to the server using Symfony and KnpSnappy. I don't want my pdf to download to the browser. I want it to save to the server. Please help! Thanks so much.
Server Path: $pdfFolder = $_SERVER['DOCUMENT_ROOT'].'/symfonydev/app/Resources/account_assets/'.$account_id.'/pdf/';
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($content),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="'.$pdfFolder.''.strtotime('now').'.pdf"'
)
);
As you can see from documentation you need to use another function:
$this->get('knp_snappy.pdf')->generateFromHtml($content, $pdfFolder . time() . '.pdf';
As you can see I replaced your strtotime('now') by time(). It will be faster.
This line generates the PDF...
$this->get('knp_snappy.pdf')->getOutputFromHtml($content)
So you could probably just use file_put_contents to write the result of getOutputFromHtml($content) to the file system.

Amazon S3 files uploaded using AWS SDK for PHP is always “application/octet-stream”?

According to the docs, contentType is optional and it will attempt to determine the correct mime-type based on the file extension. However, it never seemed to guess the mime-type and always defaults to application/octet-stream
Here's my code:
$s3 = new AmazonS3();
$opt = array( 'fileUpload' => $_FILES['file']['tmp_name'],
'storage' => Amazons3::STORAGE_REDUCED);
$r = $s3->create_object('mybucket', $_FILES['file']['name'], $opt);
Here's a screenshot of my AWS console:
How do you actually automatically set the proper Content Type without setting contentType option, or do you really have to set it manually?
Additional info: If I download a file from the console (that I originally uploaded through SDK) then upload it again using the console, the proper Content-Type is set (ex: image/gif for GIF files instead of application/octet-stream)
Try to add 'contentType' key to your options array when creating object and use 'body' instead of 'fileUpload'. For example:
$s3->create_object('MY_BUCKET', 'xml_file.xml', array(
'body' => '<xml>Valid xml content</xml>',
'contentType' => 'text/xml'
));
Successfully creates file with text/xml content type. As I know Content-Type isn't set automatically when uploading through SDK. But where's the problem to derminate mime type from PHP side and set the 'contentType' property?
I have just run into this situation where, when uploading some files, the Content-Type was not automatically detected. I couldn't figure out what the cause was until I stepped through the code watching the SDK execution. I discovered that the CFMimeTypes::get_mimetype() function, which is used to automatically determine the Content-Type, uses a case sensitive comparison to determine the MIME type. Because of this only lowercase extensions will match. If your file extension is upper or mixed case, then it will not match and fallback to the 'application/octet-stream' MIME type.
To fix change the line:
'fileUpload' => $_FILES['file']['tmp_name']
to
'fileUpload' => strtolower( $_FILES['file']['tmp_name'] )
I am using the SDK v 1.5.8.2, but there doesn't look to be any changes to this detection in the 1.5.9 or 1.5.10 releases that have both come out in the past week. I consider this to be a bug and will be filing it as such.

Cakephp media view to display media from other source

I am able to display and download files uploaded to the server using the media view.
function view($id) {
$this->view = 'Media';
$params = array('id' => $photo['url'], 'name' => $photo['name'], 'download' => false, 'extension' => 'png', 'path' => 'files' . DS . 'user' . DS . $user['id']);
$this->set($params);
}
But I also have an option where users can update a url(like amazon s3) of the file. Is there any way I can update the path (in the above function) to display the file.
I appreciate all your help.
Thanks
No, because either your server is serving those files, or s3 (for example) does.
So you have two options, and I'm using s3 for the example:
either download the file from s3 to your server and serve it yourself via Media
or, redirect the request to s3. For downloads you can set the Content-Type and Content-Disposition by appending &response-content-type=application/octet-stream&response-content-disposition=attachment to the url, but you can't change the filename like you can with Media

Categories