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.
Here's that HTML:
<a href="http://mysite.com/uploads/my-archive.zip">download zip</a>
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.
so I'd like my HTML to be this:
<a href="/canDownload">download zip</a>
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");
}
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.
If you answer my question, it is likely you can answer this question too: Zipped file with PHP results in cpgz file after extraction
CPGZ is a compressed archive that combines the Copy In, Copy Out archive format and gzip compression. It is similar to a TGZ file, which incorporates both an uncompressed TAR container and gzip compression; most often used on Unix-based systems such as Linux and Mac OS X.
Adoption: CPGZ files are widely used on Unix-based systems such as MacOS and Linux. Apart from the ditto utility, in MacOS you can decompress and open these archives using the built-in Apple Archive Utility program or by installing The Unarchiver.
How to Open a CPGZ File. CPGZ files are typically seen on macOS and Linux operating systems. The ditto command-line tool is one way you can open one in those systems. However, if you're running Windows, we suggest trying PeaZip, 7-Zip, or some other file compression/decompression program that supports GZ compression.
The answer in my case was that there was an empty line being output before readfile().
So i added:
ob_end_clean();
readfile($filename);
But you should probably search for the place where this line is being output in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With