1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//============ Start of file Ziping function Code ===========================================================// |
4
|
|
|
|
5
|
|
|
$zip_folder = ""; |
6
|
|
|
|
7
|
|
|
class zipper { |
8
|
|
|
|
9
|
|
|
public function LoadZipFiles($source) { |
10
|
|
|
|
11
|
|
|
if (!file_exists($source)) { |
12
|
|
|
return false; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
$source = str_replace('\\', '/', realpath($source)); |
16
|
|
|
|
17
|
|
|
$a = array(); |
18
|
|
|
|
19
|
|
|
if (is_dir($source) === true) { |
20
|
|
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); |
21
|
|
|
|
22
|
|
|
foreach ($files as $file) { |
23
|
|
|
$file = str_replace('\\', '/', $file); |
24
|
|
|
|
25
|
|
|
// Ignore "." and ".." folders |
26
|
|
|
if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..'))) |
27
|
|
|
continue; |
28
|
|
|
|
29
|
|
|
$file = realpath($file); |
30
|
|
|
|
31
|
|
|
if (is_dir($file) === true) { |
32
|
|
|
$a[] = array( |
33
|
|
|
'type' => 'dir', |
34
|
|
|
'source' => str_replace($source . '/', '', $file . '/'), |
35
|
|
|
'file' => $file, |
36
|
|
|
'size' => 0 |
37
|
|
|
); |
38
|
|
|
} else if (is_file($file) === true) { |
39
|
|
|
$src = str_replace($source . '/', '', $file); |
40
|
|
|
$size = filesize($file); |
41
|
|
|
|
42
|
|
|
$a[] = array( |
43
|
|
|
'type' => 'file', |
44
|
|
|
'source' => $src, |
45
|
|
|
'file' => $file, |
46
|
|
|
'size' => false != $size ? $size : 16000 // this is fallback in case no size |
|
|
|
|
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $a; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function ProcessZip($foldercontent, $folder, $maxsize) { |
56
|
|
|
|
57
|
|
|
$split = array(); |
58
|
|
|
|
59
|
|
|
$splits = 1; |
60
|
|
|
$t = 0; |
61
|
|
|
|
62
|
|
|
// Determine how many zip files to create |
63
|
|
|
if ( isset( $foldercontent ) ) { |
64
|
|
|
foreach ($foldercontent as $entry) { |
65
|
|
|
|
66
|
|
|
$t = $t + $entry['size']; |
67
|
|
|
|
68
|
|
|
if ($entry['type'] == 'dir') { |
69
|
|
|
$lastdir = $entry; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($t >= $maxsize) { |
73
|
|
|
$splits++; |
74
|
|
|
$t = 0; |
75
|
|
|
// create lastdir in next archive, in case files still exist |
76
|
|
|
// even if the next file is not in this archive it doesn't hurt |
77
|
|
|
if ($lastdir !== '') { |
|
|
|
|
78
|
|
|
$split[$splits][] = $lastdir; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$split[$splits][] = $entry; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// delete the $foldercontent array |
86
|
|
|
unset($foldercontent); |
87
|
|
|
|
88
|
|
|
// Create the folder to put the zip files in |
89
|
|
|
$date = new DateTime(); |
90
|
|
|
$tS = $date->format('YmdHis'); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// Process the splits |
93
|
|
|
foreach ($split as $idx => $sp) { |
94
|
|
|
|
95
|
|
|
// create the zip file |
96
|
|
|
|
97
|
|
|
$zip = new ZipArchive(); |
98
|
|
|
|
99
|
|
|
$destination = $folder . '.zip'; |
100
|
|
|
|
101
|
|
|
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$i = 1; |
106
|
|
|
$dir = ""; |
107
|
|
|
foreach ($sp as $entry) { |
108
|
|
|
if ($entry['type'] === 'dir') { |
109
|
|
|
$dir = explode('\\', $entry['file']); |
110
|
|
|
$zip->addEmptyDir(end($dir)); |
111
|
|
|
} else { |
112
|
|
|
$zip->addFromString(end($dir).'/'.$i.'.jpg', file_get_contents($entry['file'])); |
|
|
|
|
113
|
|
|
$i++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
$zip->close(); |
117
|
|
|
} |
118
|
|
|
return array( |
119
|
|
|
'splits' => count($split), |
120
|
|
|
'foldername' => '' |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getMemoryLimit() { |
126
|
|
|
$memory_limit = ini_get('memory_limit'); |
127
|
|
|
|
128
|
|
|
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) { |
129
|
|
|
if ($matches[2] == 'M') { |
130
|
|
|
$memory_limit = $matches[1] * 1024 * 1024; // nnnM -> nnn MB |
131
|
|
|
} else if ($matches[2] == 'K') { |
132
|
|
|
$memory_limit = $matches[1] * 1024; // nnnK -> nnn KB |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $memory_limit; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function make_zip($album_download_directory) { |
140
|
|
|
$zipfilename = ""; |
141
|
|
|
if ( isset( $album_download_directory) ) { |
142
|
|
|
//$zipfilename = 'libs/resources'.DIRECTORY_SEPARATOR.'albums'.DIRECTORY_SEPARATOR.'fb-album_'.date("Y-m-d").'_'.date("H-i-s"); |
143
|
|
|
$zipfilename = 'lib/resources/albums/fb-album_'.date("Y-m-d").'_'.date("H-i-s"); |
144
|
|
|
|
145
|
|
|
// name of folder starting from the root of the webserver |
146
|
|
|
// as in Wordpress /wp-content/themes/ (end on backslash) |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
$folder = dirname($_SERVER['PHP_SELF']).'/'.$album_download_directory; |
150
|
|
|
|
151
|
|
|
// Server Root |
152
|
|
|
$root = $_SERVER["DOCUMENT_ROOT"]; |
153
|
|
|
|
154
|
|
|
// source of the folder to unpack |
155
|
|
|
$sourcedir = $root . $folder; // target directory |
156
|
|
|
|
157
|
|
|
// Don't use more than half the memory limit |
158
|
|
|
$memory_limit = $this->getMemoryLimit(); |
159
|
|
|
$maxsize = $memory_limit / 2; |
160
|
|
|
|
161
|
|
|
// Is zipping possible on the server ? |
162
|
|
|
if (!extension_loaded('zip')) { |
163
|
|
|
echo 'Zipping not possible on this server'; |
164
|
|
|
exit; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Get the files to zip |
168
|
|
|
$foldercontent = $this->LoadZipFiles($sourcedir); |
169
|
|
|
if ($foldercontent === false) { |
170
|
|
|
echo 'Something went wrong gathering the file entries'; |
171
|
|
|
exit; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Process the files to zip |
175
|
|
|
$zip = $this->ProcessZip($foldercontent, $zipfilename, $maxsize); |
176
|
|
|
if ($zip === false) { |
177
|
|
|
echo 'Something went wrong zipping the files'; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// clear the stat cache (created by filesize command) |
181
|
|
|
clearstatcache(); |
182
|
|
|
|
183
|
|
|
require_once( 'unlink_directory.php' ); |
184
|
|
|
$unlink_directory = new unlink_directory(); |
185
|
|
|
$unlink_directory->remove_directory( $album_download_directory ); |
186
|
|
|
} |
187
|
|
|
return $zipfilename; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function get_zip( $album_download_directory ) { |
191
|
|
|
$response = '<span style="color: #ffffff;">Sorry due to some reasons albums cannot be downloaded.</span>'; |
192
|
|
|
if ( isset( $album_download_directory ) ) { |
193
|
|
|
$zip_folder = $this->make_zip( $album_download_directory ); |
194
|
|
|
if ( !empty( $zip_folder ) ) { |
195
|
|
|
$response = '<a href="' . $zip_folder . '.zip" id="download-link" class="btn btn-success link-buttons-border-color" >Download Zip Folder</a>'; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
return $response; |
199
|
|
|
} |
200
|
|
|
} |