打开下载的zip文件会创建cpgz文件吗? [英] Opening downloaded zip file creates cpgz file?

查看:511
本文介绍了打开下载的zip文件会创建cpgz文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我为一个zip文件的url创建一个链接的 href 并单击该链接,我的zip文件会被下载并打开它获取我期望的内容。

If I make the url for a zip file the href of a link and click the link, my zip file gets downloaded and opening it gets the contents as I expect.

这是HTML:

<a href="http://mysite.com/uploads/my-archive.zip">download zip</a>

问题是我想指向我的应用程序的链接,以便我可以确定是否用户有权访问此zip文件。

The problem is I'd like the link to point to my application such that I could determine whether the user is authorized to access this zip file.

所以我希望我的HTML是这样的:

so I'd like my HTML to be this:

 <a href="/canDownload">download zip</a> 

我的PHP为 / canDownload 页面:

//business logic to determine if user can download

if($yesCanDownload){

$archive='https://mysite.com/uploads/my-archive.zip';
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=".basename($archive));
header("Content-Length: ".filesize($archive));
ob_clean();
flush();
echo readfile("$archive");
}   

所以,我认为这个问题与 header()代码,但我尝试了一些基于各种谷歌和其他SO建议相关的东西,但没有工作。

So, I think the problem has to do with the header() code but i've tried a bunch of things related to that based on various google and other SO suggestions and none work.

如果你回答我的问题,你可能也可以回答这个问题:带有PHP的压缩文件在提取后生成cpgz文件

If you answer my question, it is likely you can answer this question too: Zipped file with PHP results in cpgz file after extraction

推荐答案

好的,我自己回答了问题。

Ok, I answered my own question.

我最初没有说清楚的主要问题是该文件不在我的应用服务器上。它位于亚马逊AWS s3存储桶中。这就是为什么我在我的问题中使用了一个完整的URL, http:// mysite ... 而不仅仅是服务器上的文件路径。事实证明 fopen()可以打开网址(所有s3存储桶对象,也就是文件,都有网址)这就是我所做的。

The main problem, which I originally didn't make clear, was that the file was not located on my application server. It was in a Amazon AWS s3 bucket. That is why I had used a full url in my question, http://mysite... and not just a file path on the server. As it turns out fopen() can open urls (all s3 bucket "objects", a.k.a. files, have urls) so that is what I did.

这是我的最终代码:

$zip= "http://mysite.com/uploads/my-archive.zip"; // my Amazon AWS s3 url
header("Content-Type: archive/zip"); // works with "application/zip" too
header("Content-Disposition: attachment; filename='my-archive.zip"); // what you want to call the downloaded zip file, can be different from what is in the s3 bucket   
$zip = fopen($zip,"r"); // open the zip file
echo fpassthru($zip); // deliver the zip file
exit(); //non-essential

这篇关于打开下载的zip文件会创建cpgz文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆