MicroFrameWork/kernel/helpers/Files.php
2024-10-08 17:06:14 +03:00

85 lines
2.3 KiB
PHP

<?php
namespace kernel\helpers;
use FilesystemIterator;
use ZipArchive;
class Files
{
public function copy_folder($d1, $d2): void
{
if (is_dir($d1)) {
if (!file_exists($d2)){
$_d2 = mkdir($d2, permissions: 0755, recursive: true);
if (!$_d2) {
return;
}
}
$d = dir($d1);
while (false !== ($entry = $d->read())) {
if ($entry != '.' && $entry != '..') {
$this->copy_folder("$d1/$entry", "$d2/$entry");
}
}
$d->close();
} else {
copy($d1, $d2);
}
}
public function recursiveRemoveDir($dir): void
{
$includes = new FilesystemIterator($dir);
foreach ($includes as $include) {
if(is_dir($include) && !is_link($include)) {
$this->recursiveRemoveDir($include);
}
else {
unlink($include);
}
}
rmdir($dir);
}
public function pack(string $source, string $destination, bool $include_source = true): void
{
$zip = new ZipArchive();
$zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($include_source) {
$zip->addEmptyDir(basename($source));
}
if (is_file($source)) {
$zip->addFile(basename($source));
}
if (is_dir($source)) {
$this->recursiveAddFile($zip, $source);
}
$zip->close();
}
private function recursiveAddFile(ZipArchive $zip, string $dir): void
{
$includes = new FilesystemIterator($dir);
// Debug::prn($includes);
foreach ($includes as $include) {
// var_dump($include->getFilename());
if(is_dir($include)/* && !is_link($include)*/) {
var_dump('ЭТО ПАПКА');
// $d = dir($include);
// $entry = $d->read();
// if ($entry == '.' || $entry == '..') continue;
$zip->addEmptyDir(basename($include));
$this->recursiveAddFile($zip, $include);
}
else {
var_dump("ЭТО ФАЙЛ");
$zip->addFile($include);
}
}
}
}