When developing Web applications, you may encounter files in different formats-CSV data, password files, XML-encoded content, and binary data in different forms. Your PHP script will frequently interact with these files, read data from them, and write data into them. Because some files in these formats need to be processed, you should not be surprised that there are so many types of built-in functions and external libraries in PHP, used to connect and use almost all file formats that you can name.
This PHP guide for creating ZIP files is about such a file format, which may be encountered by application developers almost every day: ZIP format. Generally, this format is used to transmit files through email and remote connection. It can compress multiple files into one file. Therefore, the hard disk space of files is reduced, and it is easier to move them. PHP can read and create these ZIP files through its ZZipLib plug-in and the Archive_Zip class of PEAR.
I will assume that you already have Apache running properly, installed PHP, And the PEAR Archive_Zip class has been correctly installed.
Note: You can directly install the PEAR Archive_Zip package from the Internet, download it, or use the provided instructions.
Create a ZIP file in PHP
Let's start with a simple example: Create a ZIP file that includes several other files dynamically. Start with the script in list.
List
- < ?php
- include ('Archive/Zip.php');
- // imports
- $obj = new Archive_Zip('test.zip');
- // name of zip file
- $files = array('mystuff/ad.gif',
- 'mystuff/alcon.doc',
- 'mystuff/alcon.xls');
- // files to store
- if ($obj->create($files)) {
- echo 'Created successfully!';
- } else {
- echo 'Error in file creation';
- }
- ?>
The above are tips for creating ZIP files in PHP.