Create New ZIP Archive

This section provides a tutorial example on how to create a new ZIP archive and add files into the archive using the ZipArchive class.

The best way to create a new ZIP archive is to use the "ZipArchive::CREATE" flag in the $zip->open() method. For example, the following code will create a new empty ZIP archive, if '/tmp/' does not exist. If '/tmp/' exists, it will be open to add new entries or extract entries. It fails only if '/tmp/' exists and is not a ZIP file.

$zip = new ZipArchive;

Here is an example script that create a new ZIP archive and adds some files into the archive.

#  zip-create-new.php
#- Copyright (c) All Rights Reserved.

  $zip = new ZipArchive;
  echo "Creating\n";
  $rc = $zip->open('/tmp/',ZipArchive::CREATE);
  if ($rc === TRUE) {
    echo " opened\n";

    $file = 'dot.gif';
    if ($zip->addFile($file) === TRUE) {
      echo "   $file added\n";
    } else {
      echo "   Failed to add $file.\n";

    $file = 'zip-create-new.php';
    if ($zip->addFile($file) === TRUE) {
      echo "   $file added\n";
    } else {
      echo "   Failed to add $file.\n";

    $file = 'junk.jnk';
    if ($zip->addFile($file) === TRUE) {
      echo "   $file added\n";
    } else {
      echo "   Failed to add $file.\n";

  } else {
    echo "   Failed to create with error: $rc\n";

Let's try it:

herong$ php zip-create-new.php
Creating opened
   dot.gif added
   zip-create-new.php added
   Failed to add junk.jnk.

herong$ unzip -l /tmp/
Archive:  /tmp/
  Length      Date    Time    Name
---------  ---------- -----   ----
       43  03-01-2020 14:15   dot.gif
      815  03-01-2020 15:17   zip-create-new.php
---------                     -------
      858                     2 files

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending out Emails

 Image and Picture Processing

Managing ZIP Archive Files

 The ZipArchive Class

Create New ZIP Archive

 Exact Files from ZIP Archive

 Create ZIP Archive with Directory

 Create ZIP Archive for Download

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Outdated Tutorials


 Full Version in PDF/EPUB