打开下载的 zip 文件会创建 cpgz 文件吗?

2024-08-22php开发问题
2

本文介绍了打开下载的 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> 

和我的 /canDownload 页面的 PHP:

and my PHP for the /canDownload page:

//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() 代码有关,但我根据各种 google 和其他 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.

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

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 文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17