0

I am trying to download a .tgz file using the following:

$file = '/var/www/upload/myfile.tgz';

if (file_exists($file))
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
}

The problem is that when I 'm trying to extract the downloaded file with tar gives me the following error:

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

note: this error does not occur when trying to extract the original file.

sitilge
  • 3,687
  • 4
  • 30
  • 56
orestiss
  • 2,183
  • 2
  • 19
  • 23
  • the archive is a tar, not a GZip archive. – Junaid Ahmed May 06 '15 at 08:06
  • Unpack a tar without the z, it is for gzipped (compressed), only: – Junaid Ahmed May 06 '15 at 08:06
  • 2
    http://stackoverflow.com/questions/15744023/how-to-extract-filename-tar-gz-file – Saty May 06 '15 at 08:07
  • @JunaidAhmed , @saty Im using `tar xf myfile.tgz ` which is working for the original but not for the downloaded – orestiss May 06 '15 at 08:12
  • 2
    check the exact filesize of the two, or do a compare of the files if you have the tools – BobbyTables May 06 '15 at 08:15
  • Are you sure that you don't output anything else before the readfile? (you can disable output buffering and see if php shows errors) – Thomas May 06 '15 at 09:31
  • @Thomas I 'm not sure how to disable output buffering, I tried with an htaccess but didn't work – orestiss May 07 '15 at 09:15
  • You can add ```ob_end_flush()``` and the beginning of the download script. If you see errors that you can not add header information it should show you where you already started to output something ahead of the download. – Thomas May 07 '15 at 12:19

1 Answers1

0

It worked when I moved the code to a separate .php file

orestiss
  • 2,183
  • 2
  • 19
  • 23
  • Than it seems that in your old php file you echoed some chars, e.g. Whitespaces at the beginning or so in front of your headers and exit calls. – take May 07 '15 at 09:00