php force download zip file

I'm trying to create a zip file, then force download of the file. See attached files.

build_zip_all.php - attempt to  create zip & download.
create_zip.php - Got this off the web as a zip file creator
download.php - php script that does download a single file; lower part of build_zip_all.php was created from this.

In the "calling" program, the reference is (in Javascript):
if (x == "a") {
                  window.location="build_zip_all.php?path=prod_docs/" + dir + "&f1=" + drawing + "&f2=" + brochure + "&f3=" + spec + "&vid=" + vid + "&from=sumary-new.php";
            }      

When I test this I get:

PKqQ5Gˆ¼(Ùe summaries/100098summary.pdfS pqÓ5Ô3æ2V0PÈOÊⲱѩ,HUÐHLOå’E©y% †@Ù .ý ÔâüÒ¢äÔb#ˆ€s~^ P¾XÁÄ·³ãJÍK™b‚0Í-3§$µHAß-'±$Õ%59?h¸Oj^zI†‚©‘9POqIQjb.WÅœ©S£óg;´Þÿ«ýÌp¢Ì®7­§“s,_éöÎVðžúÉ)Ñgålý'þ~}4CÕLà÷Ñì%–÷Ï̳ÕÙ×סSq±ûµ•ƒÎü¿®+Ë­H1á|py.ï”è‡ -‘›<ÿÞWÜÇRY»Kp}·SÄ!6•­Û”uÜ-ö-s8|õò: ÃöåZ睷în{°žíÐùZq.3ÓX)®í6lMœ§ï7Ý•rÔ{¨wÖKtÚÊÙÂ7y}keX¶ò/8l0#1$)G£·…«jÉÌ×]M™mµbUe'MÖKóùÑ}æ\Öã¢þÊ¥›¶:vsdW¥}b•;,¤_ØVýÏñÇl­P gö¯¦õ_zÍQÞºÐÝè|¡èk¶ïFr›–,åZŸ×±à“ûŽþW•K…j¬_q ¶|½ˆ‡u™êÏV×ýêWòÛw­þÀ`2uûdÖ½G˜ws^uþÉ.j³&›¹Ë[&¡îñã\‘ÄÀ%û7ßj3}•à}Õ¾Ìû‡ó·[ö[Ûz攁¤GôêiÖ)«óžÿÿë ßã[Jg¶qÜ¿TĐ»*ý²«}ˆÉÄ9çâž¾f\²2÷ýÿÃÎÉgX—^Ü=ûYÏãEkJc¯pÈ~É]b´NðV‰ÎŽåÑ_~;ô¹ËžÝÇ²ÎùÞB{“–_¾]Ì‘m7ëüŸÏütm_놛3&V¨ÙznÓ´Ä%Å{š}U½ÿ²NæÚö[Á.~¶ÜF鯻÷€’4e@•!¶$Z̥R¬ JÀA ± 4Y J±\ú¾©)™‰Nù Ñ@9sK#=3CË…H«¦èƺ5—¾Sbq*ˆ¥ ï‘šS–Z’™œÈ¥\šTVR ´Â5˜ž3óÒôÃ3óóŠ3aHæ›m¾®S~N y–Á-æÙ¢üäàÔ…h}`æš’Z´Ä3XNPÚJ{ƒ ì67CSH®v3R0³€æëGø'e¥&ƒ•¹ÍQmL)Mær 7•†zfš\ú!™%9© Á•Å%©¹ Á¥¹¹‰E•@qg`œ–dæç¹Ë +#CSK#CCCCcM$+,¬€sbIbN~:$êaÅ’Ajžc2ÈLX2ЏˆŒRÈ+ÍɁ†±>‰•ù¥ÀÀðÏKuÎÏ)ÍÍC²¬¢(5Ë@Á’ËÌLMMÒ`bfÀ‚ ,“³4³ÀŠbŠY˜cˆ™›bˆY˜a¨34°0Â3´@Ò[R”˜™“Z©à̪T 7ôƒòóK, !ä™—–¯`‹Óâ’Ä¢° , ¸TU]ýݸPKqQ5Gˆ¼(Ùe summaries/100098summary.pdfPKIž

Which I am guessing is the contents of the zip file.

The zip file IS created in the folder zips; eg: 1442848297allfiles.zip

What am I doing wrong?
build_zip_all.php
create_zip.php
download.php
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Following code for me works fine:
	header( "Content-Type: application/octet-stream" );
	header( "Content-Length: " . filesize( $zipfile) );
	header( "Content-Disposition: attachment; filename=" . basename( $zipfile) );
	readfile( $filename );

Open in new window

Julian HansenCommented:
Instead of doing a readfile of the ZIP - why not simply redirect to the URL to the zip

header('location: path/to/zip/zipfile.zip');

Open in new window


The browser and server should take care of the rest
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
To Marco Gasi,

I changed my code to reflect yours & ran it. It produces nothing (blank page). I'm not surprised, the variable $filename is not defined.

Modified source attached.
build_zip_all.php
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
To Julian Hansen,

Because:

(1) The zip file does not exist & needs to be built.

(2) I want the effect to be "stay on the page" you are on.

Or maybe your approach works if I build the zip file ahead of time in anticipation of this being needed?
Marco GasiFreelancerCommented:
Sorry. Just replace $filename with $zipfile

readfile( $zipfile);

Open in new window

Julian HansenCommented:
(1) The zip file does not exist & needs to be built.
Understood but you are creating the file anyways - simply point a URL to it. Nothing changes from what you have right now except instead of readfile you redirect to the actual file itself
(2) I want the effect to be "stay on the page" you are on.
You will stay on the same page - when a request is made for a file that is to be saved (like a zip) i.e. not displayed in the browser - the user is presented with a Save As dialog and the page remains the same as the one that initiated the download - in other words this requirement is met by default.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
The 'readfile' example should still work with a 'zip' file to force a download.  http://php.net/manual/en/function.readfile.php
<?php
$file = 'monkey.gif';

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;
}
?>

Open in new window

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
I used a variation of your solution; I created the zip file FIRST, then used the download.php; worked perfectly.

Thanks
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.