Php readfile()的替代方案
我目前正在使用以下代码从我的AmazonS3帐户下载文件Php readfile()的替代方案,php,amazon-s3,amazon-ec2,readfile,Php,Amazon S3,Amazon Ec2,Readfile,我目前正在使用以下代码从我的AmazonS3帐户下载文件 if(isset($_POST['file_name'])) { $bucket = $_POST['bucket']; $fname = $_POST['file_name']; $accessKey = 'AKIAIEHFBME6F5Q62FTQ'; $secretKey = '<REMOVED>'; $file_type = pathinfo($fname, PATHINFO_EXTENSION); $mp3_url
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";
}
if(isset($\u POST['file\u name'])){
$bucket=$_POST['bucket'];
$fname=$\u POST['file\u name'];
$accessKey='akaiehfbme6f5q62ftq';
$secretKey='';
$file\u type=pathinfo($fname,pathinfo\u扩展名);
$mp3\u url=el\u s3\u getTemporaryMP3Link($accessKey、$secretKey、$bucket、$fname);
$zip_url=el_s3_getTemporaryZipLink($accessKey、$secretKey、$bucket、$fname);
如果($file_type==“mp3”){
标题(“内容类型:音频/mpeg3”);
标题('Content-Disposition:attachment;filename=“themixtapesite”.$fname.”);
readfile($mp3\u url);
退出();
}else if($file_type==“zip”){
标题(“内容类型:应用程序/zip”);
标题('Content-Disposition:attachment;filename=“themixtapesite”.$fname.”);
readfile($zip_url);
退出();
}否则{
echo“对不起,文件不存在”;
}
}
如您所见,它检查文件扩展名,然后将相关url传递到readfile()
我使用S3的全部目的是减轻EC2服务器的负载。
但是,它似乎使用这种方法下载文件,并通过EC2服务器从S3中拉取文件(通过iStat监控服务器负载显示下载文件时活动增加)
有没有其他方法可以直接从S3下载文件,而不是通过EC2服务器
更新:只是一个快速更新-我使用的是Nginx而不是apache,所以如果有人想建议的话,请不要使用.htaccess。如果您有一个可公开访问的url或签名的url。您可以使用位置重定向将其直接传递给客户端
header('Location:'.$myUrl)代码>谢谢,但我的问题是,如果文件是MP3,它只在浏览器中播放。我想强制下载那个文件。我想内容配置:附件
应该告诉浏览器下载文件而不是播放文件,但如果我将readfile()
更改为header('location…
),它似乎没有任何作用。您应该能够在s3上设置文件的标题。我从未尝试过以这种方式设置内容处置标头,但它可能会起作用。抱歉,您在那里丢失了我?当您在s3上存储文件时,您还可以设置1个或多个标头。下载时,这些标题将随文件一起传递。您应该能够设置一个内容处理头,以允许直接从s3下载文件。啊,对了,我不知道您可以设置其他头。我会留意的。谢谢