Alternative to readfile()

2019-07-19 07:43发布

I'm currently using the following code to download files from my Amazon S3 account.

if(isset($_POST['file_name'])) {
$bucket = $_POST['bucket'];
$fname = $_POST['file_name'];
$accessKey = 'AKIAIEHFBME6F5Q62FTQ';
$secretKey = '<REMOVED>';
$file_type = pathinfo($fname, PATHINFO_EXTENSION);

$mp3_url = el_s3_getTemporaryMP3Link($accessKey, $secretKey, $bucket, $fname);    
$zip_url = el_s3_getTemporaryZipLink($accessKey, $secretKey, $bucket, $fname);


if ($file_type === "mp3") {
    header('Content-type: audio/mpeg3');
    header('Content-Disposition: attachment; filename="themixtapesite_'.$fname.'"');
    readfile($mp3_url);
    exit();
} else if ($file_type === "zip") {
    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="themixtapesite_'.$fname.'"');
    readfile($zip_url);
    exit();
} else {
    echo "Sorry, File does not exist";
}

}

As you can see it checks what the file extension is then passes the relevant url in to read file()

The whole point of me using S3 is to lighten my EC2 server loads. However, it seems using this method to download the files it pulling the files from S3 via the EC2 server (monitoring the server loads via iStat shows me an increase in activity when downloading a file).

Is there an alternative way of doing this so the file is downloaded directly from S3 rather than via the EC2 server??

UPDATE: Just a quick update - I'm using Nginx rather than apache so no .htaccess if anybody was thinking of suggesting that.

1条回答
别忘想泡老子
2楼-- · 2019-07-19 08:30

If you have a publicly accessible url, or signed url. You can pass that directly to the client using a location redirect.

header('Location: ' . $myUrl);

查看更多
登录 后发表回答