Forcing download of tar.gz results in corrupt file


I’ve tried various different combinations of headers force the download of a tar.gz file.
I’ve also tried copying the file and forcing the download of the copied file after opening and closing it.

Every attempt has resulted in a corrupt file.

Currently, i’m running the scripts locally, so i’ve checked the actual file and opens fine, but like i said, the downloaded version is always corrupt.

Has anyone got any working header combinations?


header(‘Content-disposition: attachment; filename=yourfile.tar.gz’);
header(‘Content-type: application/x-gzip’);

the tar type shouldnt matter; the external wrapper is a gzip.


Thanks @StarLion, I tried your code but im still receiving corrupt files.
I also tried it with relative paths.

This is my code:

$tarfile = new Archive_Tar( /* absolute path to tar location */ );
if( $tarfile->create( $files ) )
	header('Content-disposition: attachment; filename=' . basename( /* absolute path to tar location */ ) );
	header('Content-type: application/x-gzip');
	readfile( self::EXPORT_DIRECTORY . basename( /* absolute path to tar location */ ) );
	unlink( self::EXPORT_DIRECTORY . basename( /* absolute path to tar location */ ) );
} else {
	throw new Exception( 'Failed to create archive' );

I’ve tried it without the unlink, but that made no difference.



Ignore my previous post, your code does work.
It must be something prior to the download, etc which is causing the issue.


There was a legacy script which was causing issues.
I ‘solved’ the issue using output buffering.