I compressed a folder with with a function and the file (compressed.zip) has all the content of the folder. I tested it to download and everything works fine. Now i want to automatically download the file after compression has done.
This is the code i use for that:
if(isset($_GET['compress'])) {
// compress folder and sent compressed zip to plugins/sfm/views
Zip($_GET['compress'], "plugins/sfm/views/compressed.zip");
if(file_exists('plugins/sfm/views/compressed.zip')){
// set example variables
$filename = "compressed.zip";
$filepath = "plugins/sfm/views/";
// http headers for zip downloads
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/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
}
}
Like i said; when i go to plugins/sfm/views/compressed.zip via the url he extracts the folder without problem and all the content is inside it with extraction. But the forced download gives me a compressed.zip that seems to be corrupted.
When trying to open the download with Winrar, he says: Unexpected end of archive. and: the archive is either in unknown format or damaged. Why the forced download gives me this error? The zip file is compressed correctly!