How to create a zip archive using PHP
TAGS: PHP, ZIP, Archive


<?php
if (isset($_POST['dozip']))  { 
$thedir = getcwd(); 
$zippath = $thedir . "/" . "zippo.zip";	 

$zip = new ZipArchive(); 
if ($zip->open($zippath, ZipArchive::CREATE)) {	 
	$zip->addFile("file_1.txt");	 
	$zip->addFile("file_2.txt");	 
	$zip->addFile("file_3.txt");	 
} 						 
$zip->close(); 

echo "Finished"; exit(); 
} 
?> 

<HTML><BODY> 
<TABLE> 
<FORM METHOD='POST' ACTION=''> 
<TR> 
<TD>Click here to create ZIP archive: </TD> 
<TD><INPUT TYPE='submit' NAME='dozip' VALUE='Create zip archive'></TD> 
</TR></FORM> 
</TABLE> 
</BODY></HTML> 


This snippet shows how easy it is to create a .zip archive using PHP. Remember to have the three text files present in your directory.