Wednesday, March 23, 2011

Forcing to download a zip file in all browsers

Friends,
Have you ever gotten a chance to force the browser for downloading a zip file? Hopefully yes. You will not get any issue unless and untill it contains multiple zip files i.e. nested zip files. Behavior is also different for IE and firefox. Below is the code to download a zip file(may be single level or nested)

<?php
 ob_clean();
 $yourfile="rnd_source.zip"; // file which is stored on the server
 $dfile="rnd.zip";  // name of the file which will be downloaded
 $fp = @fopen($yourfile, 'rb');
 if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
 {
        header('Content-Type: "application/x-zip-compressed"');
        header('Content-Disposition: attachment; filename="'.$dfile.'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: ".filesize($yourfile));
}
else
{
        header('Content-Type: "application/x-zip-compressed"');
        header('Content-Disposition: attachment; filename="'.$dfile.'"');
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header("Content-Length: ".filesize($yourfile));
}

fpassthru($fp);
fclose($fp);
?>
Provide your valuable comments please.............if you get any issue or wanna anything to share.

Important queries based formulas to watch performance of a MySQL based application

(1) Session Vs global status variables value select G.variable_name,G.variable_value GlobalValue,S.variable_value SessionValue from (selec...