ZIP is an archive file format that holds compressed files. It's a convenient way to keep a bunch of files together when storing or sharing them. When coding, you can make your PHP codes work with ZIP files as well.
Unlike a lot of other functions we have talked about in our previous lessons, PHP ZIP file function set doesn't come inbuilt. Luckily, if you are using PHP 5 or a newer version, the extensions needed are free to download and easy to install. We'll start our tutorial with links and instructions for doing just that.
PHP Zip: Main Tips
- These functions allow PHP scripts to work with ZIP files.
- To use these functions, you must install the corresponding libraries.
First Steps of Installation Process
There are two libraries you must install for PHP ZIP file functions to work in your scripts:
For Linux (PHP 5+)
PHP Zip functions and library are not inbuilt by default, so you have to download them both from the sources listed above. To include ZIP support, --with-zip=DIR configuration option should be used.
Theory is great, but we recommend digging deeper!
For Windows (PHP 5+)
Same as in Linux, PHP Zip functions and library are not supported by default, so you have to download the PHP Zip extensions listed. Then, remember to enable php_zip.dll in php.ini.
Note: If you're thinking of enabling any PHP Zip extension, keep in mind the extension_dir setting (found inside the php.ini) has to be set to the file location of the PHP Zip extension you want to use.
List of Functions
Look at the table below to see all of the PHP Zip function names. All of them have been introduced in PHP 4 and supported in all following versions:
|zip_close()||Close PHP ZIP files|
|zip_entry_close()||Close PHP ZIP file entries.|
|zip_entry_compressedsize()||Return size of PHP ZIP entries, as compressed|
|zip_entry_compressionmethod()||Return compression method of PHP ZIP entries|
|zip_entry_filesize()||Return real file size of entries in PHP ZIP files|
|zip_entry_name()||Return name of entry in PHP ZIP file|
|zip_entry_open()||Open entry in PHP ZIP files in order to read|
|zip_entry_read()||Read from currently open entries in PHP ZIP file|
|zip_open()||Make PHP unzip file (open it)|
|zip_read()||Read the following entry in PHP ZIP file|
Note: Since version 5.2, you can also use a PHP ZipArchive class to zip and unzip files.
PHP Zip: Summary
- Working with ZIP files in PHP codes requires using special functions.
- These methods don't come inbuilt, so you'll have to install PHP Zip extensions to use them. They are free to download and use though.