/
make_zip.php
56 lines (43 loc) · 1.45 KB
/
make_zip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
//------------------------------------------------------
function createZip($files, $zip_file) {
$zip = new ZipArchive;
if($zip->open($zip_file, ZipArchive::OVERWRITE) === TRUE) {
foreach($files as $file){
$zip->addFile($file);
}
$zip->close();
return true;
}
else return false;
}
//------------------------------------------------------
/* Example */
if(isset($_POST['files']) && !empty($_POST['files'])) {
$files = array();
foreach ($_POST['files'] as $file) {
$files = array_merge($files, glob($file));
}
// for preventing directory traversal, allow only from a specific directory ie /var/www/html/ClimateProxiesFinder_DB
$files_checked = array();
foreach ($files as $file) {
if (strpos(realpath($file), "/var/www/html/ClimateProxiesFinder_DB") !== false) {
array_push($files_checked, $file);
}
}
//print_r($files);
//print_r($files_checked);
$zip_file = tempnam('/tmp', 'ClimateProxiesFinder_YourSelection_').'.zip';
if (createZip($files, $zip_file)) {
header('Content-Description: File download');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($zip_file).'"');
header('Pragma: no-cache');
header('Expires: 0');
readfile($zip_file);
unlink($zip_file);
exit;
}
}
//------------------------------------------------------
?>