This post helps you to create a ZIP file using PHP, Arun had coded a few lines of script that system converts the selected files into ZIP file format. It is useful for ecommerce web projects like selling PDFs, Images and Docs ect, use can choose files and download it into compressed format. Take a look at this live demo
Download Script Live Demo
Developer
HTML Code
Form contains list of files with input type checkbox name files[].
<form name="zips" method="post">
<input type="checkbox" name="files[]" value="flowers.jpg" />
<img src="files/image.png" />
flowers.jpg
<input type="checkbox" name="files[]" value="fun.jpg" />
<img src="files/image.png" />
fun.jpg
<input type="checkbox" name="files[]" value="9lessons.docx" />
<img src="files/doc.png" />
9lessons.docx
........
<input type="submit" name="createpdf" value="Download as ZIP" />
<input type="reset" name="reset" value="Reset" />
</form>
<input type="checkbox" name="files[]" value="flowers.jpg" />
<img src="files/image.png" />
flowers.jpg
<input type="checkbox" name="files[]" value="fun.jpg" />
<img src="files/image.png" />
fun.jpg
<input type="checkbox" name="files[]" value="9lessons.docx" />
<img src="files/doc.png" />
9lessons.docx
........
<input type="submit" name="createpdf" value="Download as ZIP" />
<input type="reset" name="reset" value="Reset" />
</form>
PHP Code
Contains PHP code covert the selected files into ZIP file format.
<?php
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file)
{
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file)
{
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
Nice ! Thanx..
ReplyDeleteSimple yet useful stuff. Thanks Arun for this awesome php tutorial.
ReplyDeletenice, thank you :)
ReplyDeleteAwsum thanks
ReplyDeleteVery nice! thanx
ReplyDeletegreat tutorial man.........thanx
ReplyDeletegoo.gl/FqxGx
thank you
ReplyDeletevery nice..
ReplyDeleteThank You, Nice Script.
ReplyDeleteNice..!!
ReplyDeleteI'm waiting for your tutorial on node.js
ReplyDeleteGracias, muy bien.!
ReplyDeleteMuy bueno, Gracias!
ReplyDeleteNice, thanks.........
ReplyDeletevery useful!
ReplyDeleteNot working . I get this error.
ReplyDeleteResponse denied by WatchGuard HTTP proxy.
Reason: body content-type denied rule='ZIP archive'
Method: POST
Host: demos.9lessons.info
Path: /zipper/index.php
Try to fix it.
Thanks for your great post.
Great
ReplyDeleteMuchas gracias...!!!
ReplyDeleteThank You, Nice Script
as always great post !
ReplyDeleteThanks Arun
ReplyDeletethanks very much.
ReplyDeleteReally Nice code
ReplyDeleteThanks Arun
Thanks Arun...its great...
ReplyDeleteHi Srinivas ,
ReplyDeleteGood tutorial but i would like so much if you write some about Android.
great thanks
ReplyDeleteThanks, Srinivas Tamada
ReplyDeleteVery nice!
ReplyDeleteI have stock think download all file.
ReplyDeleteI am course fix again lock security password done open download much.
thanks for your programming a great.
This tutorial is great!
ReplyDeleteDo you need to activate any PHP extensions to get this to work?
ReplyDeletenice code!thanks!
ReplyDeletegreat thanks for this one
ReplyDeleteThanx..
ReplyDeleteNot working my localhost
ReplyDeletestill showing
* You dont have ZIP extension
nice tutorial
ReplyDeletetq so much :)
Cool tutorial
ReplyDeleteVery, very good tutorial
ReplyDeleteGood luck Arun
how to set it zips folder with the data?
ReplyDeleteWell you can also extract that file in .rar file. i can say that good tutorial you have shared that information.
ReplyDeleteI would like to export multiple files in one folder in zipu
ReplyDeleteVery Nice tutorial :)
ReplyDeletevery nice tutorial
ReplyDeleteReally Nice Tutorial With Coding..
ReplyDeletevaibhav Lahamge, You need to enable the php zip extension on your local machine.
ReplyDeleteThanks for the zip code to , Btw Arun.
@vaibhav make sure u have ZIP extension.
ReplyDeletecool tutorial...
ReplyDeleteNice post. I manage to get it work with your tutorial.
ReplyDeleteHowever, I realised the zip file will turn out corrupted if the total filesize is more than 2.5mb
Any idea how to resolve this?
Actully, was looking for something like that and I got this amazing tutorial really it help me a lot .
ReplyDeleteKeep posting ...thanks for this share :-)
Hi,
ReplyDeletei m getting below error:
* You dont have ZIP extension
please help me out for this.
Deepak
You are giving the wonderful info through this blog so keep updating this,
ReplyDeletesimply super
ReplyDeleteGreat!
ReplyDeletethanx
ReplyDeleteThanks :)
ReplyDeleteHi,
ReplyDeleteI use this function to create a Zip archive, with multiple files:
// function to create ZIP archive. Returns TRUE on success, otherwise, False
// receives 2 arguments: an array with the path-name of the files to add in archive,
// and the path-name of the Zip file that will be created
function createZip($files, $zip_file) {
// PHP-MySQL Course - http://www.coursesweb.net/php-mysql/
// create an object of the ZipArchive class
$zip = new ZipArchive;
// if the $zip_file can be created, traverse the array $files and add each file in archive
if($zip->open($zip_file, ZipArchive::CREATE) === TRUE) {
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
return true;
}
else return false;
}
nothing to say bro just cool and cool love u alot :)
ReplyDeleteWhere is ZipArchive class ???
ReplyDeleteAwww dude, you so totally rock. Phatt script man, mucho thank you. Saved me a good chunk of time reading docs and phuttsing around.
ReplyDeletenice code
ReplyDeleteGreat Post...Thanks...
ReplyDeleteThanks for this nice tutorial.
ReplyDeletethanx arun
ReplyDeletenice thank....
ReplyDeleteNice dude! It works great for my project..:)
ReplyDeleteHi Arun & Srinivas,
ReplyDeletewonderful work of you.
Can you please tell how to give Absolute URL of the file ??
Thank you!
Thank you for posting this Arun.
ReplyDeleteIf you don't want the zip file to inherit the directory structure you can change...
$zip->addFile($file_folder.$file);
To ...
$zip->addFile($file_folder.$file,pathinfo($file,PATHINFO_BASENAME));
can we control compression method?? i mean i want compress a file to minimum size ..
ReplyDeleteHi thanks lot for your jobs :) ! Your script works fine but I have zipped files and folders, you have an idea about the changes in the script that I can do what I want to do?
ReplyDeletenice one post thnx for update :)
ReplyDeletenot work on mp3 ??
ReplyDeleteCan someone help me convert a Link Within php file to a zip so I can add it as a plug in to my wordpress.org site? thanks!!
ReplyDeleteHi its charan.
ReplyDeleteI have implemented zip download using php.
i am trying to download more the 50 recording means mp3 files. its not executing the zip achiver..
if i give exact 50 recording. its executing zip..
Can i get suggestions on this..
ReplyDeletethis one is sort and sweet
=================================
//however you get the path
$yourfile = "http://test.com/15.pdf";
$file_name = basename($yourfile);
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));
readfile($yourfile);
exit;
========================================
http://business-directory-list.com
have any send me the code for ZIPARCHIVE class. i need ZIPARCHIVE class file code...
ReplyDeleteI click F5 or reload button browser, but the field with the "error" remains in place.
ReplyDeleteI need help solution to this problem.
Hi
ReplyDeleteThis is very Helpful Tutorial....
Mai I know how to make this ZIP with Password Protected?
Thanks
Excellent. Easy to use & modify to requirements
ReplyDeleteInvaluable when it comes to choir members downloading their inividual Training Files, rather that having to plod through individual hrefs (although I've included hrefs for those that prefer the old way).
Thanks a lot. Just what I needed
Hi,
ReplyDeleteat first thanks for your script this is simple and easy to understand
when i am download so I try to open the downloaded zip file the crc32 is always 00000000 and the file/files cannot be extracted. I tried doing the same thing with text files and that works fine. (the crc32 is 000000000)
ReplyDeletebut i am download the zip file through ftp so zip file is extracted
when i am download so I try to open the downloaded zip file the crc32 is always 00000000 and the file/files cannot be extracted. I tried doing the same thing with text files and that works fine. (the crc32 is 000000000)
ReplyDeletebut i am download the zip file through ftp so zip file is extracted
the prob is solved used a die(); after unlink($zip_name);
ReplyDeleteor
the prob is solved used a die(); after unlink($zip_name); and code is past on top of haeder.php
eg
unlink($zip_name);
die();
i want to use another web site link to download. how to give path.
ReplyDeleteu r used $file_folder = "files/"; // folder to load files
i want to use another web link to my server
i am doing this php in my server that is skytouch.com but i want to give file folder to www.somthing.com
ReplyDeletehow to give file path for this
pls help me.........
0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file)
{
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
i want to add file_folder path to another url
ReplyDeletehow it possible
Thanks nice code
ReplyDeleteThanks, been looking for a simple example for ages
ReplyDeleteHi,
ReplyDeletei m getting below error:
* You dont have ZIP extension
please help me out for this.
vijay
hey thanks guys. this what i need..its working awesome..
ReplyDeletehi superbimaster
ReplyDeleteNice post. I manage to get it work with your tutorial.
ReplyDeleteGreat Post! works fine! hou can I do this from sql query? thanks in advance
ReplyDeleteI got zip error when open but ashwani solved with:
ReplyDeleteunlink($zip_name);
die();
Thanks!
It works very well on linux and windows but not on apple systems! Can you help in this situation?
ReplyDeleteNice
ReplyDeleteI don't want to create folder in zip, what should i change ?
ReplyDeleteThank you.
I have an image in one valued value = "apartment_spg18inter2.jpg'apartemen_spg35inter1.jpg'apartemen_spg55view3.jpg '"
ReplyDeleteHow to zip the image?