-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_zip.php
83 lines (59 loc) · 1.56 KB
/
new_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
$path = $_POST['path'];
$array = $_POST['array'];
$zip_file = $_POST['name'].".zip";
$val = true;
// Initialize archive object
$zip = new ZipArchive();
$zip->open("$path/$zip_file", ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach ($array as $value){
if(is_dir("$path/$value")){
if(file_exists("$path/.partial")){
}else{
mkdir("$path/.partial");
}
rename("$path/$value" , "$path/.partial/$value");
$dir = "$path/.partial";
$rootPath = realpath($dir);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
}else{
// Add Files Too
$zip->addFile("$path/$value", "$value");
}
}
// Zip archive will be created only after closing object
$zip->close();
if(file_exists("$path/.partial")){
$inside_folder = scandir("$path/.partial");
foreach ($inside_folder as $my_file){
if($my_file != "." && $my_file != ".."){
rename("$path/.partial/$my_file" , "$path/$my_file");
}
}
$val = rmdir("$path/.partial");
}
if(file_exists("$path/$zip_file")){
if($val == true){
echo "success";
}else{
//For Debug
}
}else{
echo "error";
}
?>