-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzip.php
More file actions
72 lines (61 loc) · 1.38 KB
/
zip.php
File metadata and controls
72 lines (61 loc) · 1.38 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
set_time_limit (0);
$ret = Zip('.');
?><!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Zip</title>
</head>
<body>
<?php
if ($ret === false) {
?><p>Error.</p><?php
} else {
?><p>Done: <tt><?=$ret?></tt></p><?php
}
?>
</body>
</html><?php
function Zip($source, $target = null)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
if($target === null){
$target = basename(realpath($source)) . '.' . uniqid() . '.zip';
}
$zip = new ZipArchive();
if (!$zip->open($target, ZIPARCHIVE::CREATE)) {
return false;
}
$target = realpath($target);
$source = realpath($source);
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$path = realpath($file->getPathname());
$localpath = str_replace($source . DIRECTORY_SEPARATOR, '', $path);
$file = realpath($file);
if($path === $target || $path === __FILE__){
continue;
}
if (is_dir($file) === true)
{
$zip->addEmptyDir($localpath);
}
else if (is_file($file) === true)
{
$zip->addFile($path, $localpath);
}
}
}
else if (is_file($source) === true)
{
$zip->addFile(basename($source), $source);
}
$zip->close();
return $target;
}