inital commit
This commit is contained in:
50
app/Utility/createDirZip.php
Normal file
50
app/Utility/createDirZip.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utility;
|
||||
use ZipArchive;
|
||||
use RecursiveIteratorIterator;
|
||||
use RecursiveDirectoryIterator;
|
||||
|
||||
class createDirZip {
|
||||
|
||||
|
||||
public function testZip($source, $destination) {
|
||||
// Get real path for our folder
|
||||
$rootPath = realpath($source);
|
||||
|
||||
// Initialize archive object
|
||||
$zip = new ZipArchive();
|
||||
$zip->open($destination, 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
|
||||
if(!empty($filePath) && !empty($relativePath)){
|
||||
$zip->addFile($filePath, $relativePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Zip archive will be created only after closing object
|
||||
return $zip->close();
|
||||
}
|
||||
|
||||
|
||||
private function testLog($data){
|
||||
$data = json_encode($data);
|
||||
$file = fopen(WWW_ROOT."dropbox/test.txt","w");
|
||||
echo fwrite($file,$data);
|
||||
fclose($file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user