从url获取zip文件(PHP)

从url获取zip文件(PHP),php,amazon-web-services,amazon-s3,download,zip,Php,Amazon Web Services,Amazon S3,Download,Zip,我有一个zip文件,我希望用户能够下载。诀窍是我不想让用户看到url是什么,我也不想把文件下载到我的服务器上 因此,我希望用户单击如下链接: http://example.com/download/4 $path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file $mm_type="application/x-compressed"; // modify accordi

我有一个zip文件,我希望用户能够下载。诀窍是我不想让用户看到url是什么,我也不想把文件下载到我的服务器上

因此,我希望用户单击如下链接:

http://example.com/download/4
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();
哪个服务器端使用此url访问我的S3存储桶:

https://s3.amazonaws.com/my-bucket/uploads/4.zip

我在
download($file\u id)
函数中尝试了使用S3方法的cURL和各种
headers()。这一定很容易,对吧

你的权利,这很容易。也许你必须写这样的东西:

http://example.com/download/4
$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/x-compressed"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");

readfile($path); // outputs the content of the file

exit();

您可以设置各种标题,让用户下载.zip文件。之后,使用
readfile()
将文件放入输出缓冲区,然后出于安全考虑,使用
exit()
结束脚本。这应该适合你!请记住更改文件路径。

谢谢@Xatenev的帮助。这实际上是我的工作:

$path = '/my-bucket/uploads/4.zip'; // the file made available for download via this PHP file
$mm_type="application/zip"; // modify accordingly to the file type of $path, but in most cases no need to do so

header("Content-Type: " . $mm_type);
header('Content-Disposition: attachment; filename="'.basename($path).'"');
readfile($path); // outputs the content of the file

exit();

哦-xou是否再次尝试添加其他标题?也许只是mime_类型不正确?据我所知,你似乎要讨论的每一件事都会涉及到将文件下载到你的服务器。一个简单且看似显而易见的解决方案是生成并返回一个签名URL,该URL的过期时间很短,允许浏览器直接从S3下载文件。拥有一个将在几秒钟内过期的url和拥有一份文件副本之间没有明显的区别,因此如果您对这种方法有一些担心,请解释。@Michael sqlbot这是一个很好的观点。“简单而显而易见的解决方案是生成并返回一个签名URL”。我同意这是显而易见的解决办法。您的方法的问题是失去了url的品牌,以及使您的bucket结构公开。对于一个爱好网站来说,这些并不重要,但对于一个企业来说,它们可以成为现实。