For a future project I needed these days some easy to use zip or gzip class to create a zip file from files / folders inside a specified directory. A short search on Google has lead me to the Create ZIP File PHP class from Rochak Chauhan. I tested two other scripts before and must say that this script works great for single files if you add them manually.

To compress a whole directory with an unknown number of files into one zip file I created some class extension to get this job done.

Extend the PHP class script

I used some directory functions (opendir, readdir) to get all the files from the specified directory. Inside each directory there are files and “real directories” and directory locations. We need only the real directories. We test each value with “is_file”, “is_dir” and we filter off the directory directions (“.” and “..”). After the “file” is identified, the files content is added to the “addFile” object and for other directories the method “get_file_from_folder” is called again.

class createDirZip extends createZip { function get_files_from_folder($directory, $put_into) { if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if (is_file($directory.$file)) { $fileContents = file_get_contents($directory.$file); $this->addFile($fileContents, $put_into.$file); } elseif ($file != '.' and $file != '..' and is_dir($directory.$file)) { $this->addDirectory($put_into.$file.'/'); $this->get_files_from_folder($directory.$file.'/', $put_into.$file.'/'); } } } closedir($handle); } }

To get the files and sub directories from some directory we use this code:

$createZip = new createDirZip; $createZip->addDirectory('themes/'); $createZip->get_files_from_folder('blog/wp-content/themes/', 'themes/');

We need create an object from the extension we have just created. Next we need only to define the base directory (which is used in the zip file) and the path to the directory. With this we are able to output the zip file:

$fileName = 'tmp/archive.zip'; $fd = fopen ($fileName, 'wb'); $out = fwrite ($fd, $createZip->getZippedfile()); fclose ($fd); $createZip->forceDownload($fileName); @unlink($fileName);

Via the method “getZippedfile” all content is added to the file (archive.zip) and the method “forceDownload” will send the created zip file to the browser.

I think this simple function is very helpful if you like to offer the public some dynamic files inside a directory or just to “zip – mail – backup” some directories from your web server using CRON.

Some important update!

After using the same script on two of my websites I got some complains from some (mac) users with the problem that they can’t open the downloaded zip files. While the download via Firefox on Windows and Linux (even most of the time with IE) works fine is this not a the best solution. I checked some other classes but didn’t find a similar solution. My advice is to use the Linux zip application via the command line: exec(‘zip -r test.zip your_folder/’); This is not the same as in this tutorial but will work for most of the machines. Actually is this solution much better even for bigger files…

Published in: PHP Scripts