Creating ZIP File with PHP.
Wall Script
Wall Script
Monday, June 11, 2012

Creating ZIP File with PHP.

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

Creating  ZIP File with PHP.

Download Script     Live Demo

Developer
Arun Kumar Shekar
Arun Kumar Sekar
Engineer
Chennai, INDIA

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" />&nbsp;
<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";
}
?>
web notification

96 comments:

  1. Simple yet useful stuff. Thanks Arun for this awesome php tutorial.

    ReplyDelete
  2. great tutorial man.........thanx
    goo.gl/FqxGx

    ReplyDelete
  3. I'm waiting for your tutorial on node.js

    ReplyDelete
  4. Nice, thanks.........

    ReplyDelete
  5. Not working . I get this error.
    Response 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.

    ReplyDelete
  6. Muchas gracias...!!!
    Thank You, Nice Script

    ReplyDelete
  7. Really Nice code
    Thanks Arun

    ReplyDelete
  8. Thanks Arun...its great...

    ReplyDelete
  9. Hi Srinivas ,
    Good tutorial but i would like so much if you write some about Android.

    ReplyDelete
  10. Thanks, Srinivas Tamada

    ReplyDelete
  11. I have stock think download all file.
    I am course fix again lock security password done open download much.

    thanks for your programming a great.

    ReplyDelete
  12. Do you need to activate any PHP extensions to get this to work?

    ReplyDelete
  13. great thanks for this one

    ReplyDelete
  14. Not working my localhost
    still showing
    * You dont have ZIP extension

    ReplyDelete
  15. nice tutorial
    tq so much :)

    ReplyDelete
  16. Very, very good tutorial
    Good luck Arun

    ReplyDelete
  17. how to set it zips folder with the data?

    ReplyDelete
  18. Well you can also extract that file in .rar file. i can say that good tutorial you have shared that information.

    ReplyDelete
  19. I would like to export multiple files in one folder in zipu

    ReplyDelete
  20. Really Nice Tutorial With Coding..

    ReplyDelete
  21. vaibhav Lahamge, You need to enable the php zip extension on your local machine.

    Thanks for the zip code to , Btw Arun.

    ReplyDelete
  22. @vaibhav make sure u have ZIP extension.

    ReplyDelete
  23. Nice post. I manage to get it work with your tutorial.

    However, I realised the zip file will turn out corrupted if the total filesize is more than 2.5mb

    Any idea how to resolve this?

    ReplyDelete
  24. Actully, was looking for something like that and I got this amazing tutorial really it help me a lot .

    Keep posting ...thanks for this share :-)

    ReplyDelete
  25. Hi,

    i m getting below error:

    * You dont have ZIP extension

    please help me out for this.

    Deepak

    ReplyDelete
  26. You are giving the wonderful info through this blog so keep updating this,

    ReplyDelete
  27. Hi,
    I 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;
    }

    ReplyDelete
  28. nothing to say bro just cool and cool love u alot :)

    ReplyDelete
  29. Where is ZipArchive class ???

    ReplyDelete
  30. Awww dude, you so totally rock. Phatt script man, mucho thank you. Saved me a good chunk of time reading docs and phuttsing around.

    ReplyDelete
  31. Nice dude! It works great for my project..:)

    ReplyDelete
  32. Hi Arun & Srinivas,
    wonderful work of you.
    Can you please tell how to give Absolute URL of the file ??

    Thank you!

    ReplyDelete
  33. Thank you for posting this Arun.
    If 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));

    ReplyDelete
  34. can we control compression method?? i mean i want compress a file to minimum size ..

    ReplyDelete
  35. Hi 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?

    ReplyDelete
  36. Can 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!!

    ReplyDelete
  37. Hi its charan.
    I 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..

    ReplyDelete

  38. this 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

    ReplyDelete
  39. have any send me the code for ZIPARCHIVE class. i need ZIPARCHIVE class file code...

    ReplyDelete
  40. I click F5 or reload button browser, but the field with the "error" remains in place.
    I need help solution to this problem.

    ReplyDelete
  41. Hi

    This is very Helpful Tutorial....

    Mai I know how to make this ZIP with Password Protected?

    Thanks

    ReplyDelete
  42. Excellent. Easy to use & modify to requirements
    Invaluable 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

    ReplyDelete
  43. Hi,
    at first thanks for your script this is simple and easy to understand

    ReplyDelete
  44. 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)

    but i am download the zip file through ftp so zip file is extracted

    ReplyDelete
  45. 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)

    but i am download the zip file through ftp so zip file is extracted

    ReplyDelete
  46. the prob is solved used a die(); after unlink($zip_name);

    or

    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();

    ReplyDelete
  47. i want to use another web site link to download. how to give path.

    u r used $file_folder = "files/"; // folder to load files

    i want to use another web link to my server

    ReplyDelete
  48. i am doing this php in my server that is skytouch.com but i want to give file folder to www.somthing.com

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

    ReplyDelete
  49. i want to add file_folder path to another url
    how it possible

    ReplyDelete
  50. Thanks, been looking for a simple example for ages

    ReplyDelete
  51. Hi,

    i m getting below error:

    * You dont have ZIP extension

    please help me out for this.
    vijay

    ReplyDelete
  52. hey thanks guys. this what i need..its working awesome..

    ReplyDelete
  53. Nice post. I manage to get it work with your tutorial.

    ReplyDelete
  54. Great Post! works fine! hou can I do this from sql query? thanks in advance

    ReplyDelete
  55. I got zip error when open but ashwani solved with:
    unlink($zip_name);
    die();
    Thanks!

    ReplyDelete
  56. It works very well on linux and windows but not on apple systems! Can you help in this situation?

    ReplyDelete
  57. I don't want to create folder in zip, what should i change ?
    Thank you.

    ReplyDelete
  58. I have an image in one valued value = "apartment_spg18inter2.jpg'apartemen_spg35inter1.jpg'apartemen_spg55view3.jpg '"
    How to zip the image?

    ReplyDelete

mailxengine Youtueb channel
Make in India
X