Skip to content

Commit 4f81257

Browse files
Added zip.php - file to zip an entire folder
1 parent a82d0e5 commit 4f81257

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

zip.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
// increase script timeout value
4+
ini_set('max_execution_time', 5000);
5+
6+
$folder = dirname(__FILE__ );
7+
8+
function show($str){
9+
echo $str . "<br/>\n";
10+
flush();
11+
ob_flush();
12+
}
13+
show($folder);
14+
15+
$date = getdate();
16+
$splitNum = 0;
17+
18+
$archive = $folder."/temporary/backup_" . $date[0];
19+
$currentArchive = $archive . "_" . $splitNum . ".zip";
20+
21+
$zip = new ZipArchive();
22+
if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) {
23+
die ("Could not open archive");
24+
}
25+
26+
$numFiles = 0;
27+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./"));
28+
foreach ($iterator as $key=>$value){
29+
$numFiles += 1;
30+
}
31+
show( "Will backup $numFiles to $archive.zip" );
32+
33+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./"));
34+
$numFiles = 0;
35+
$counter = 0;
36+
$maxFilePerArchive = 500;
37+
foreach ($iterator as $key=>$value){
38+
$counter += 1;
39+
if ($counter >= $maxFilePerArchive) {
40+
$currentArchive = $archive . "_" . $splitNum++ . ".zip";
41+
show( "Too many files: splitting archive, new archive is $currentArchive" );
42+
$zip->close();
43+
$zip = new ZipArchive();
44+
if ($zip->open($currentArchive, ZIPARCHIVE::CREATE) !== TRUE) {
45+
die ("Could not open archive");
46+
}
47+
$counter = 0;
48+
}
49+
//$i = $maxFilePerArchive*$splitNum + $counter;
50+
if (! preg_match('/temporary\/backup_' . $date[0] . '/', $key)){
51+
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
52+
$numFiles += 1;
53+
if ($numFiles % 300 == 0) {
54+
show( "$numFiles" );
55+
}
56+
} else {
57+
show( "Not backuping this file -> $key" );
58+
}
59+
}
60+
// close and save archive
61+
$zip->close();
62+
show( "Archive created successfully with $numFiles files." );
63+
64+
?>

0 commit comments

Comments
 (0)