从url获取zip文件(PHP)

时间:2014-06-08 14:15:03

标签: php amazon-web-services amazon-s3 download zip

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

所以我希望用户点击这样的链接:

http://example.com/download/4

哪个服务器端使用此URL访问我的S3存储桶:

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

我已尝试过cURL,使用S3方法,以及headers()函数中的各种download($file_id),但无法使其正常工作。这很简单,对吧?

2 个答案:

答案 0 :(得分:2)

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

$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()结束脚本。这对你有用!请记住更改文件的路径。

答案 1 :(得分:0)

感谢@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();