March 27, 2019

zipper script for WordPress

function zipper(){
if(isset($_GET[‘zipper’])){

if(!class_exists(‘ZipArchive’))
echo ‘ZipArchive – X<br />’;

if(isset($_GET[‘folder’])){
$click_here = ”;
$folder = $_GET[‘folder’];
ini_set(‘max_execution_time’, 5000);

// create object
$zip = new ZipArchive();

$root = (isset($_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’])?$_SERVER[‘SUBDOMAIN_DOCUMENT_ROOT’]:$_SERVER[‘DOCUMENT_ROOT’]).(isset($_GET[‘sub’])?’/’.$_GET[‘sub’]:”);
// open archive

$uploads = false;
$max = $uploads?12:1;

for($i=1; $i<=$max; $i++){
echo $downloadable_file = $root.’/’.str_replace(array(‘/’), ‘-‘, $folder).’-‘.$i.’.zip’;//exit;
$click_here = ‘<a href="’.str_replace($root, ”, $downloadable_file).’">Save Now</a>’;
echo file_exists($downloadable_file)?'<br />Done! ‘.$click_here.'<br />’:'<br />’;
if ($zip->open($downloadable_file, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}

$f = ($i>9?$i:’0′.$i);
// initialize an iterator
// pass it the directory to be processed
$dir = ($folder==’/’?$root:($root."/wp-content/".($folder!=’self’?($uploads?’uploads/’:”).$folder.($uploads?’/’.$f:”)."/":”)));
//pree($dir);
//pree(!is_dir($dir));
if(is_dir($dir)){

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
$filename = str_replace($root.’/’, ”, $key);

$basename = basename($filename);

if(!in_array($basename, array(‘.’, ‘..’))){
$parts = explode(‘.’, $basename);

$ext = end($parts);
if(!in_array($ext, array(‘zip’))){
$zip->addFile(realpath($key), $filename) or die ("ERROR: Could not add file: $key");
}

}
}
echo "Archive created successfully.<br />";
}else{

echo ‘<b>’.$dir."</b><br />Directory does not exists.";
pre($_SERVER);
}

}
// close and save archive
$zip->close();
exit;
}
}
}
zipper();

Last updated: March 27, 2019