Tips for creating ZIP files in PHP

Source: Internet
Author: User

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

 
 
  1. < ?php  
  2. include ('Archive/Zip.php'); 
  3. // imports  
  4. $obj = new Archive_Zip('test.zip'); 
  5. // name of zip file  
  6. $files = array('mystuff/ad.gif',   
  7. 'mystuff/alcon.doc',   
  8. 'mystuff/alcon.xls'); 
  9. // files to store  
  10. if ($obj->create($files)) {  
  11. echo 'Created successfully!';  
  12. } else {  
  13. echo 'Error in file creation';  
  14. }  
  15. ?> 

The above are tips for creating ZIP files in PHP.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.