1<?php
2 $error = ""; //error holder
3 if(isset($_POST['createpdf'])){
4 $post = $_POST;
5 $file_folder = "files/"; // folder to load files
6 if(extension_loaded('zip')){ // Checking ZIP extension is available
7 if(isset($post['files']) and count($post['files']) > 0){ // Checking files are selected
8 $zip = new ZipArchive(); // Load zip library
9 $zip_name = time().".zip"; // Zip name
10 if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
11 $error .= "* Sorry ZIP creation failed at this time<br/>";
12 }
13 foreach($post['files'] as $file){
14 $zip->addFile($file_folder.$file); // Adding files into zip
15 }
16 $zip->close();
17 if(file_exists($zip_name)){
18 // push to download the zip
19 header('Content-type: application/zip');
20 header('Content-Disposition: attachment; filename="'.$zip_name.'"');
21 readfile($zip_name);
22 // remove zip file is exists in temp path
23 unlink($zip_name);
24 }
25
26 }else
27 $error .= "* Please select file to zip <br/>";
28 }else
29 $error .= "* You dont have ZIP extension<br/>";
30 }
31?>
32<!DOCTYPE html>
33<html lang="en">
34<head>
35<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
36<title>Download As Zip</title>
37</head>
38<body>
39<center><h1>Create Zip</h1></center>
40<form name="zips" method="post">
41<?php if(!empty($error)) { ?>
42<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
43<?php } ?>
44<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
45 <tr>
46 <td width="33" align="center">*</td>
47 <td width="117" align="center">File Type</td>
48 <td width="382">File Name</td>
49 </tr>
50 <tr>
51 <td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td>
52 <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
53 <td>a.jpg</td>
54 </tr>
55 <tr>
56 <td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td>
57 <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
58 <td>b.jpg</td>
59 </tr>
60 <tr>
61 <td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td>
62 <td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
63 <td>c.docx</td>
64 </tr>
65 <tr>
66 <td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td>
67 <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
68 <td>d.pdf</td>
69 </tr>
70 <tr>
71 <td colspan="3" align="center">
72 <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" />
73 <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" />
74 </td>
75 </tr>
76</table>
77
78</form>
79</body>
80</html>
1#PHP 5.6: Use the --with-libzip=DIR configure option to use a system libzip installation. libzip version 0.11 is required, with 0.11.2 or later recommended.
2
3#PHP 7.3: Building against the bundled libzip is discouraged, but still possible by adding --without-libzip to the configuration.
4
5zip_close() Closes a ZIP file archive
6zip_entry_close() Closes a ZIP directory entry
7zip_entry_compressedsize() Returns the compressed file size of a ZIP directory entry
8zip_entry_compressionmethod() Returns the compression method of a ZIP directory entry
9zip_entry_filesize() Returns the actual file size of a ZIP directory entry
10zip_entry_name() Returns the name of a ZIP directory entry
11zip_entry_open() Opens a directory entry in a ZIP file for reading
12zip_entry_read() Reads from an open directory entry in the ZIP file
13zip_open() Opens a ZIP file archive
14zip_read() Reads the next file in a open ZIP file archive