-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmyTest.php
44 lines (39 loc) · 1.11 KB
/
myTest.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
<?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;
}
?>
<?php
if(!empty($_GET["file"]) && file_exists($_GET["file"]."/"))
var_dump(createZip($_GET['file']));
else
echo "none";
?>