PHP Force Upload Remote File

I have a bunch of videos stored on my Amazon S3 storage. I'm working on creating a PHP script very similar to here , where users can upload videos to their hard drive.

I would like to use something like this:

<?php
$file_name = $_GET['file'];
$file_url = 'http://www.myamazons3.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"".$file_name."\""); 
readfile($file_url);
exit;

      

However, I am under the impression that this increases the bandwidth because the video will go through my server.

Any ideas on how I can get these videos to load while avoiding reading them through my own server first?

Many thanks!

+2


source to share


4 answers


Take a look at the S3 API Docs and notice the header values ​​you can set. Amazon will send them when the file is requested: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html (the same parameters can be sent via POST to update an existing object)



+2


source


The php script you specified will work fine, but the main drawback is that every time a visitor to your site requests a file, your own servers download it from S3 and then relay that data to the browser. For low traffic sites, this is probably not a big deal, but for high traffic traffic, you definitely want to avoid running everything through your own servers.

Fortunately, there is a fairly easy way to configure files to force boot from S3. You just want to set content-type and content-location (just setting content will work in some browsers, but setting both should work in all browsers).

This code assumes you are using the Amazon S3 PHP class from Undesigned:



  "application / octet-stream", "Content-Disposition" => "attachment")); ? >

Now all your files will be forced to download. You may need to clear your cache to see the change. And obviously, don't do this in any file you actually want to load in the "inline" browser.

The nice part of this solution is that apps that download media files directly (like let's say mp3 player in Flash) don't care about content type or content, so you can still play your files in browser and then link to download the same file. If the user has already finished downloading the file to flash, they will most likely still have it in their cache, which means the download will be very fast and it won't even cost you any additional bandwidth fees. from S3.

+1


source


header('Location: ' . $file_url);

      

0


source


I know this is an old thread, but over the past year, the S3 team has added support for request parameters that allow you to override certain HTTP headers upon request.

You can, for example, load the SVG image as image/svg+xml

, but then override the headers on checkout as application/octet-stream

to force the load.

Using the AWS SDK for PHP , you can use Aws \ S3 \ S3Client :: createPresignedUrl () to achieve this.

0


source







All Articles