【问题标题】:Get zip file from url (PHP)从 url (PHP) 获取 zip 文件
【发布时间】:2014-07-29 05:02:49
【问题描述】:

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

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

http://example.com/download/4

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

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

我在 download($file_id) 函数中尝试了 cURL、使用 S3 方法和各种 headers(),但无法使其正常工作。这一定很容易,对吧?

【问题讨论】:

  • 据我所知,您似乎在讨论的一切仍然涉及将文件下载到您的服务器。简单且看似显而易见的解决方案是生成并返回一个过期时间较短的签名 URL,以允许浏览器直接从 S3 直接下载文件。拥有将在几秒钟内过期的网址与拥有文件副本之间没有显着区别,因此,如果您对此方法有疑问,请解释。
  • @Michael-sqlbot 好点。 “简单且看似显而易见的解决方案是生成并返回签名 URL”。我同意这是显而易见的解决方案。您的方法的问题是失去了 url 的品牌以及使您的存储桶结构公开。这些对于业余爱好网站来说并不那么重要,但对于企业来说,它们可以成为现实。

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


【解决方案1】:

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

$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() 结束您的脚本。这应该适合你!请记住更改文件的路径。

【讨论】:

    【解决方案2】:

    感谢@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_type 不正确?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多