PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
Create ZIP Archive with Directory
This section provides a tutorial example on how to create a ZIP archive with a directory and adds files to that directory using the ZipArchive class.
If you want create a directory structure inside the ZIP archive so that files can be added to the archive to different locations, you need to two methods from the ZipArchive class:
1. Create directory inside ZIP archive using addEmptyDir() method. For example, the following code creates a directory and a sub-directory.
$zip->addEmptyDir('src'); $zip->addEmptyDir('src/gif');
2. Add file to archive with local path name using addFile() method. For example, the following code adds 'dot.gif' to the 'src/gif' sub-directory in the archive.
$zip->addFile('dot.gif', 'src/gif/dot.gif');
Here is an example script that creates a new ZIP archive with a directory and adds some files to that directory in the archive.
<?php # zip-with-directory.php #- Copyright 2009 (c) HerongYang.com. All Rights Reserved. $zip = new ZipArchive; echo "Creating test.zip:\n"; $rc = $zip->open('/tmp/test.zip',ZipArchive::CREATE); if ($rc === TRUE) { echo " test.zip opened\n"; $dir = "srcDir"; if ($zip->addEmptyDir($dir) === TRUE) { echo " $dir added\n"; } else { echo " Failed to add $dir.\n"; } $file = 'dot.gif'; if ($zip->addFile($file, "$dir/$file") === TRUE) { echo " $file added\n"; } else { echo " Failed to add $file.\n"; } $file = 'zip-create-new.php'; if ($zip->addFile($file, "$dir/$file") === TRUE) { echo " $file added\n"; } else { echo " Failed to add $file.\n"; } $zip->close(); } else { echo " Failed to create test.zip with error: $rc\n"; } ?>
Let's try it:
herong$ php zip-with-directory.php Creating test.zip: test.zip opened srcDir added dot.gif added zip-create-new.php added herong$ unzip -l /tmp/test.zip Archive: /tmp/test.zip Length Date Time Name --------- ---------- ----- ---- 0 06-04-2020 15:53 srcDir/ 43 06-04-2020 15:37 srcDir/dot.gif 815 06-04-2020 15:17 srcDir/zip-create-new.php --------- ------- 858 3 files
Table of Contents
Introduction and Installation of PHP
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
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
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
Extract Files from ZIP Archive
►Create ZIP Archive with Directory
Create ZIP Archive for Download
Managing PHP Engine and Modules on macOS