-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipCreate.php
executable file
·59 lines (49 loc) · 1.91 KB
/
zipCreate.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
57
58
59
<?php
function zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', DIRECTORY_SEPARATOR, realpath($source));
$source = str_replace('/', DIRECTORY_SEPARATOR, $source);
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
if ($file == '.' || $file == '..' || empty($file) || $file == DIRECTORY_SEPARATOR) {
continue;
}
// Ignore "." and ".." folders
if (in_array(substr($file, strrpos($file, DIRECTORY_SEPARATOR) + 1), array('.', '..'))) {
continue;
}
$file = realpath($file);
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
if (is_dir($file) === true) {
$d = str_replace($source . DIRECTORY_SEPARATOR, '', $file);
if (empty($d)) {
continue;
}
$zip->addEmptyDir($d);
} elseif (is_file($file) === true) {
$zip->addFromString(str_replace($source . DIRECTORY_SEPARATOR, '', $file),
file_get_contents($file));
} else {
// do nothing
}
}
} elseif (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
zip($argv[1], './'.uniqid().'.zip');
//var_dump($argv[1]); uniqid()
?>