-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathzip.php
More file actions
37 lines (33 loc) · 1011 Bytes
/
zip.php
File metadata and controls
37 lines (33 loc) · 1011 Bytes
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
<?php
// Get real path for our folder
function createZip($folder)
{
if(!file_exists($folder))
return false;
$rootPath = realpath("$folder/");
// Initialize archive object
$zip = new ZipArchive();
$zip->open("$folder.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$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);
}
}
// Zip archive will be created only after closing object
$zip->close();
return true;
}
?>