Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening downloaded zip file creates cpgz file?

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

like image 955
tim peterson Avatar asked Jul 15 '12 19:07

tim peterson


People also ask

What is a zip CPGZ file?

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.

What program opens CPGZ files?

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 do I unzip a CPGZ file in Windows?

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.


1 Answers

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.

like image 175
carlosvini Avatar answered Oct 01 '22 00:10

carlosvini