Alternative to readfile()

折月煮酒 提交于 2019-12-12 16:40:12

问题


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:


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);



来源:https://stackoverflow.com/questions/15006380/alternative-to-readfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!