Code has been added to clipboard!

Correct Usage of PHP ZIP: Set up Guide and Function List Included

Reading time 3 min
Published Aug 8, 2017
Updated Oct 3, 2019

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.

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:

Function Description
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.
Tutorial
Introduction
Installation
Syntax
Variable
Superglobals
Data Types
String
Array
Multidimensional Array
Sort Array
Constant
Operators
Cookies
Sessions
DateTime
Error Handling
Exception Handling
File
Write and Create File
File Open, Read and Close
File Upload
Filtering
Redirecting
Advanced Filters
Forms
Form Required Field
Validate Email/URL
Form Validation
Form Action
Function
Prepared Statements
JSON
Calendar
ZIP File
FTP
HTTP Response
DateTime Functions
Error Functions
File Function
Filter
Math Functions
Mail Function
Miscellaneous Functions
Date Format
String Functions
Array Functions
Directory Functions
MySQL Database
MySQL Connection
MySQL Create Database
MySQL Create Table
MySQL Delete Data
MySQL Insert Data
MySQL Get Last Record ID
MySQL Insert Multiple Records
MySQL Select Data
MySQL Limit Data
MySQL Update Data
MySQLi Functions
AJAX and MySQL
AJAX Search
AJAX Poll
RSS Reader
Read XML File in PHP
XML Parser
SimpleXML Parser
SimpleXML: Node and Attribute
Expat XML Parser
DOMDocument
Libxml Functions
SimpleXML Functions
XML Parsing Functions
PHP isset
PHP echo and print
PHP if else and elseif
PHP switch case
PHP include File
PHP while Loop
PHP for and foreach
PHP mail()
PHP explode()
PHP substr()
PHP str_replace()
PHP array_push
PHP count()