php test ZipArchive using cli code example
Example 1: php create zip from folder
<?php
class FlxZipArchive extends ZipArchive
{
public function addDir($location, $name)
{
$this->addEmptyDir($name);
$this->addDirDo($location, $name);
}
private function addDirDo($location, $name)
{
$name .= '/';
$location .= '/';
$dir = opendir ($location);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
$do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($location . $file, $name . $file);
}
}
}
?>
<?php
$the_folder = '/path/to/folder/to/be/zipped';
$zip_file_name = '/path/to/zip/archive.zip';
$za = new FlxZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($the_folder, basename($the_folder));
$za->close();
}
else{
echo 'Could not create a zip archive';
}
?>
Example 2: ziparchive php example
<?php
$error = "";
if(isset($_POST['createpdf'])){
$post = $_POST;
$file_folder = "files/";
if(extension_loaded('zip')){
if(isset($post['files']) and count($post['files']) > 0){
$zip = new ZipArchive();
$zip_name = time().".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file);
}
$zip->close();
if(file_exists($zip_name)){
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}
}else
$error .= "* Please select file to zip <br/>";
}else
$error .= "* You dont have ZIP extension<br/>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip</title>
</head>
<body>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
<tr>
<td width="33" align="center">*</td>
<td width="117" align="center">File Type</td>
<td width="382">File Name</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>a.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td>
<td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
<td>b.jpg</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td>
<td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
<td>c.docx</td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td>
<td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
<td>d.pdf</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as ZIP" />
<input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>