Home  >  Article  >  Backend Development  >  A brief analysis of techniques for creating ZIP archive files with PHP_PHP Tutorial

A brief analysis of techniques for creating ZIP archive files with PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-22 09:01:521086browse

When developing web applications, there is a good chance that you will come across files in different formats - CSV data, password files, XML encoded content and different form of binary data. Your PHP scripts will need to frequently interact with these files, reading data from and writing data to them. With so many file formats to deal with, it's not surprising that PHP has so many types of built-in functions and external libraries for connecting to and using almost any file format you can name.

This guide to creating ZIP archive files in PHP is about such a file format that application developers may encounter almost every day: ZIP format. Typically used to transfer files via email and remote connections, this format compresses multiple files into a single archive, thereby reducing the files' hard drive footprint and enabling them to be moved more easily. PHP can read and create these ZIP files through its ZZipLib plug-in and PEAR's Archive_Zip class.

I am going to assume that you already have a functioning Apache, PHP installed, and the PEAR Archive_Zip class is installed correctly.

Note: You can install the PEAR Archive_Zip package directly from the web, or download it, or use the instructions provided.

Create ZIP archive with PHP

Let’s start with a simple example: dynamically create a ZIP archive that includes several other files. Start with the script in Listing A.

List A

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>include ('Archive/Zip.php'); </span></li><li><span>// imports  </span></li><li class="alt"><span>$</span><span class="attribute">obj</span><span> = </span><span class="attribute-value">new</span><span> Archive_Zip('test.zip'); </span></li><li class="alt"><span>// name of zip file  </span></li><li><span>$</span><span class="attribute">files</span><span> = </span><span class="attribute-value">array</span><span>('mystuff/ad.gif',   </span></li><li class="alt"><span>'mystuff/alcon.doc',   </span></li><li><span>'mystuff/alcon.xls'); </span></li><li><span>// files to store  </span></li><li class="alt"><span>if ($obj-</span><span class="tag">></span><span>create($files)) {  </span></span></li>
<li><span>echo 'Created successfully!';  </span></li>
<li class="alt"><span>} else {  </span></li>
<li><span>echo 'Error in file creation';  </span></li>
<li class="alt"><span>}  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>

The above are the relevant tips for creating ZIP archive files in PHP.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445910.htmlTechArticleWhen developing web applications, it is very likely that you will encounter files in different formats such as CSV data, password files, XML encoded content and different forms of binary data. Your PHP script will need...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn