Creating ZIP File with PHP.
Wall Script
Follow Me:
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";
}
?>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


97 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 Post
    Thanks

    ReplyDelete
  5. Nice, thanks.........

    ReplyDelete
  6. 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
  7. Muchas gracias...!!!
    Thank You, Nice Script

    ReplyDelete
  8. Really Nice code
    Thanks Arun

    ReplyDelete
  9. Thanks Arun...its great...

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

    ReplyDelete
  11. Thanks, Srinivas Tamada

    ReplyDelete
  12. 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
  13. Do you need to activate any PHP extensions to get this to work?

    ReplyDelete
  14. great thanks for this one

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

    ReplyDelete
  16. nice tutorial
    tq so much :)

    ReplyDelete
  17. Very, very good tutorial
    Good luck Arun

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

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

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

    ReplyDelete
  21. very nice tutorial

    ReplyDelete
  22. Really Nice Tutorial With Coding..

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

    Thanks for the zip code to , Btw Arun.

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

    ReplyDelete
  25. 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
  26. 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
  27. Hi,

    i m getting below error:

    * You dont have ZIP extension

    please help me out for this.

    Deepak

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

    ReplyDelete
  29. 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
  30. nothing to say bro just cool and cool love u alot :)

    ReplyDelete
  31. Where is ZipArchive class ???

    ReplyDelete
  32. 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
  33. Great Post...Thanks...

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

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

    Thank you!

    ReplyDelete
  36. 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
  37. can we control compression method?? i mean i want compress a file to minimum size ..

    ReplyDelete
  38. 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
  39. nice one post thnx for update :)

    ReplyDelete
  40. 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
  41. 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

  42. 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
  43. have any send me the code for ZIPARCHIVE class. i need ZIPARCHIVE class file code...

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

    ReplyDelete
  45. Hi

    This is very Helpful Tutorial....

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

    Thanks

    ReplyDelete
  46. 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
  47. Hi,
    at first thanks for your script this is simple and easy to understand

    ReplyDelete
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. i want to add file_folder path to another url
    how it possible

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

    ReplyDelete
  55. Hi,

    i m getting below error:

    * You dont have ZIP extension

    please help me out for this.
    vijay

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

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

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

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

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

    ReplyDelete

Make in India