1

I am trying to download a zip file using php. Download work fine but when in extract zip it say invalid file. Please help me and check my below code:

apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);

$filename = "sliced Images.zip";    
$upload_dir = wp_upload_dir();

$filepath = $upload_dir['basedir'];
//echo $filepath.'/'.$filename;
//echo filesize($filepath.'/'.$filename);
//die;

// http headers for zip downloads
ob_start();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");

header("Content-Disposition: attachment; filename=\"".$filename."\"");

header("Content-Length: ".filesize($filepath.'/'.$filename));
ob_end_flush();
@readfile($filepath.'/'.$filename);

enter image description here

Addy
  • 998
  • 4
  • 12
  • 36
  • (1) Why are you suppressing warnings in `readfile` and (2) you shouldn't write anything else to the output when the script is done. Just let it terminate. If you are to use `exit` use it with no parameters. – apokryfos Aug 24 '16 at 12:59
  • Is the file 0 length when downloaded? Do you get any errors / warnings if you remove the `@` error suppressor? – Jonnix Aug 24 '16 at 13:00
  • Still not working after removing exit. – Addy Aug 24 '16 at 13:03
  • Check screen shot. Size is not zero – Addy Aug 24 '16 at 13:07
  • Do a binary comparison of original file and downloaded version. For comparison tools, see: http://stackoverflow.com/questions/8166697/tool-for-comparing-2-binary-files-in-windows – Michael Livach Aug 24 '16 at 13:22
  • Any solution for above? – Addy Aug 25 '16 at 04:39

1 Answers1

4

Above problem is solved by using below code:

header('Content-Description: File Transfer');
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="'.basename($pdf_path).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf_path));
ob_clean();
readfile($pdf_path);
Addy
  • 998
  • 4
  • 12
  • 36