Skip to content

Instantly share code, notes, and snippets.

@asvae
Created May 4, 2015 18:18
Show Gist options
  • Save asvae/496601034f8a4f143510 to your computer and use it in GitHub Desktop.
Save asvae/496601034f8a4f143510 to your computer and use it in GitHub Desktop.
<?php
$folders = [
// 'admin',
// 'boards',
'cgi-bin',
// 'cron',
// 'css',
// 'helpers',
'images',
// 'include',
'js',
'log',
// 'module',
// 'photo',
// 'test',
];
//$root = '/home/vagrant/Code/pr3'; // Asva
$root = '/var/www/zevs/data/www/99board.ru'; // 99board
//
foreach ($folders as $folder){
$path = $root .'/'. $folder;
$result = zipFolder($path, $folder);
if ($result)
echo "$folder success!<br>";
}
/**
* Implode array and die
*
* @param $array
*/
function dumpArray($array){
echo implode('<br>', $array);
die;
}
/**
* Zip files on path, don't touch subfolders.
*
* @param string $path /full/path/to/folder
* @param string $fileName
*/
function zipFilesInFolder($path, $fileName = ''){
// Initialize archive object
$zip = new ZipArchive();
$zip->open($fileName . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = glob($path. '/*');
foreach($files as $file){
if (is_file($file))
$zip->addFile($file);
}
$zip->close();
}
/**
* Zip folder and subfolders.
*
* @param $path full path
* @param $fileName archive name
* @return bool
*/
function zipFolder($path, $fileName){
// Initialize archive object
$zip = new ZipArchive();
$zip->open($fileName . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
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($path) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment