Pear
Pear

Reputation: 115

Header - Downloaded Gzip Not In Gzip Format, Correct On Server

I'm trying to create, download then remove a gzipped file.

The file created on the server is correct. I can download it with a direct link and the file decompresses correctly locally.

If I use header(), the file won't decompress and gives an error "Not In Gzip Format". It seems like I have to use header(), to be able to unlink().

The gist of the code is:

# This creates the gz file correctly..
$tar = 'meta-info.tar';
$gz = $tar.'.gz';
$p = new PharData($tar);
$p->compress(Phar::GZ)->buildFromDirectory('../');

# This creates the dialog then removes the file correctly..
header('Content-Type:'.mime_content_type($gz));
header('Content-Length:'.filesize($gz));
header("Content-Disposition:attachment;filename=$gz");
flush();
readfile($gz);
unlink($gz);

# ...but the downloaded file is not correct.

Oddly, a 'zip' file downloads and decompresses correctly.

I've looked all over stackoverflow and tried a number of things, but nothings working.. Any ideas?

Thanks in advance!

Upvotes: 0

Views: 1575

Answers (1)

Pear
Pear

Reputation: 115

The issue was that header info had already been sent.

Using ob_end_clean() fixed it (the structure of the page can't be changed).

ob_end_clean();
header("Content-Disposition:attachment;filename=$gz");
header('Content-Type:application/x-gzip');
header('Content-Length:'.filesize($gz));
flush();
readfile($gz);
unlink($gz);

Upvotes: 3

Related Questions