25 thoughts on “Tutorial: Create a zip file from folders on the fly

  1. ColdFusion Code:

    <cfzip action=”zip” source=”#ExpandPath(‘./CF8/’)#” file=”#ExpandPath(‘./CF8.zip’)#” overwrite=”true”>


  2. Hi David,
    sure PECL has a extension that do the same job, but what about the PHP users where the PECL extensions are not available?

    Can you show us a quick example like this one here?

    I checked the test script from the PECL extenstion, is there support for the zip format?

  3. Sure using a single Linux command is a good way to do it (just create a gzip file). On a lot of (shared) hosting accounts is this not possible because of missing permissions for Apache.

  4. What about using exec() to create zip files?
    Wouldn’t that be better when handling large files?

  5. That’s really cool – didn’t know it could be done !

    Just out of interest – what are you using this script for?

  6. @j7labs,

    I use it for the download of wordpress themes, if several files are updated I don’t need create a zip file over and over again. This way the demo site is using the same files than the files used for the download.

  7. Very nice and useful tutorial, thanks for sharing.

  8. In case of further details or any issue with the class, feel free to contact me.

  9. @Rochak,

    thanks for the offer! (I’m thinking about to release a PHP5 branche)

    Edit: :) the code has already a PHP5 coding style …

  10. I tried using the above technique to produce a zip archive from a directory and it all seems to have worked except the resulting zip archive is invalid. The file that’s created has a size that seems appropriate for the files that have been placed in the zip, but it won’t open. I realize this probably isn’t the best place for support, but I thought I’d leave a note and see if anyone responded. Thanks!

  11. Very nice and useful tutorial, thanks for sharing.

  12. Very nice and useful tutorial, thanks for sharing. I like this and also i give the link of this tutorial on my blog

  13. I have tried out but it does not work.May I am not using it properly.I have many hours in understanding and fixing the problem but it does not work !!

    I have made a simple php file ( 0-make-zip.php) and put up your code in it ) .(edit: removed the link)

    What I want to do is that I want take the manually take the backup of my root folder “public_html” using your script.

    Can some body help ! If it works than it will useful in way that anytime backup of a folder can be taken on the fly.

    [ I do not want to use the CPanel inbuilt option of backup as it takes the backup of all the files and folder and is time intensive ]

  14. Hi Ajay,

    i got this error on the page you have posted here:

    Parse error: syntax error, unexpected '.' in /home/ajayvibh/public_html/0-make-zip.php on line 11

    there are several possibilities that your code is not working maybe your server doesn’t support php5?

  15. Hi Olaf,

    It is on Linux server ( with Cpanel control panel ) with php5 support.

    (edit: don’t provide temp. links)

    Also I am posting at the forum same time.


  16. Hi, I had similar unexpected ‘.’ problem which I solved by changing the quotes in the code as they are not displayed here as single quotes. But I have a similar problem as I want to backup my public_html directory but when I download the zip file it has the directory (in my case backup) but no files inside, any ideas???


  17. fixed – I needed a forward slash at the end of my directory path *slaps head*

  18. Lol, Sean! I think we’ve all made those silly little mistakes at 1 time or another. Informative article even still. Kudos!

  19. Hi Vishal,

    never tried the class on windows and it looks like that the class doesn’t create a valid zip file for Mac systems. I will post an update very soon (I use the class on two websites and have this Mac problem too)

  20. I implemented whole thing as directed and got a zipped file, but when i tried to unzip it an error occured telling that it is corrupt.
    i am working on windows platform and using php5

  21. Please check the updated post…

  22. I am trying to learn how to zip files and sub files, i am taking a class on Microsoft Office and one of my assignments are to create and zip a file and 3 subfiles can someone tell me how to do so

    thank you

  23. i have download whole public_html folder as zip file but it return internal server error because public_html contain hung number of file and folder

    So whats is better solution to download this


    • Hi, check my update about using the zip command line tool. It makes no sense to create a zip file while a PHP class.

Comments are closed.