Created
September 25, 2016 11:04
-
-
Save famoser/9ea909d0ed052f8e07388d692c25038f to your computer and use it in GitHub Desktop.
zip utilities php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function rrmdir($dir) { | |
if (is_dir($dir)) { | |
$objects = scandir($dir); | |
foreach ($objects as $object) { | |
if ($object != "." && $object != "..") { | |
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); | |
} | |
} | |
reset($objects); | |
rmdir($dir); | |
} | |
} | |
rrmdir('modx'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // assuming file.zip is in the same directory as the executing script. | |
$file = 'explorer.zip'; | |
// get the absolute path to $file | |
$path = pathinfo(realpath($file), PATHINFO_DIRNAME); | |
$zip = new ZipArchive; | |
$res = $zip->open($file); | |
if ($res === TRUE) { | |
// extract it to the path we determined above | |
$zip->extractTo($path); | |
$zip->close(); | |
echo "WOOT! $file extracted to $path"; | |
} else { | |
echo "Doh! I couldn't open $file"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /** | |
* | |
* | |
* david metzger | |
dieses script packt alles, was sich im verzeichnis des scripts befindet, in ein zip und danach wird dieses zip heruntergeladen. | |
*/ | |
/** | |
* MAIN PUBLIC FUNCTIONS | |
*/ | |
/* | |
* create_zip_file($array_files) void | |
* $array_files : rückgabewert von zb get_all_files | |
* zB : array('bild.jpg','temp'.SLASH.'bla.jpg') | |
* | |
* get_all_files($dir) | |
* $dir zB 'temp' o. 'temp'.SLASH.'files' | |
* return: array mit pfaden zu den dateien; | |
* | |
* get_dir($dir) | |
* $dir zB 'temp' o. 'temp'.SLASH.'files' | |
* return: array aus fileobjects (siehe classes) | |
* holt files aus der datenbank | |
* | |
* sort_array_of_fileobjects($array_of_fileobjects[,$array_of_sort_properties]) | |
* array of fileobjects von get_dir | |
* array of sort properties in reihenfolge | |
* return: array of fileobjects | |
* | |
* table_of_fileobject_rows($array_of_fileobjects) | |
* return: string html table ohne <table .... | |
* | |
* echo_filemap([$dir]) void | |
* no return | |
* echoes eine verschachtelte ul wie ordner liste links im explorer | |
* | |
*/ | |
define('SLASH',"/"); | |
define('DIR',__DIR__.SLASH); | |
dump('basfabsdfb'); | |
$a= create_zip_file(); | |
if($a){ | |
$filee=basename($a); | |
$dirr=preg_replace('/(.*\/).*?$/i','$1',$_SERVER['PHP_SELF']); | |
$filename='http://'.$_SERVER['HTTP_HOST'].$dirr.$filee; | |
echo '<a href="'.$filename.'">Download</a>' ; | |
} | |
function create_zip_file(){ | |
$timestamp = date('y-m-d'); | |
$file = DIR.SLASH.basename(DIR).'-'.$timestamp.'.zip'; | |
$ok = Zip(DIR,$file); | |
if($ok){ | |
return $file; | |
} | |
else{ | |
return $ok; | |
} | |
} | |
function get_all_files($dir=''){ | |
$value = get_all_files_r($dir, 1); | |
return $value; | |
} | |
/*function rrmdir($dir) { | |
foreach(glob($dir . '/*') as $file) { | |
if(is_dir($file)) | |
rrmdir($file); | |
else | |
unlink($file); | |
} | |
rmdir($dir); | |
} | |
* | |
* REQUIRED (PRIVATE) FUNCTIONS | |
*/ | |
/** | |
* create_dir if it doesnt exist | |
* $path ie. = user01/bla or user2 | |
* returns true if successful, otherwise false | |
* always in DIR; | |
*/ | |
function create_dir($path,$is_general_path = 0,$contains_filename=0){ | |
$dir=''; | |
if (!$is_general_path){ | |
$path = DIR.$path; | |
} | |
$path = explode(SLASH,$path); | |
//var_dump($path); | |
if($contains_filename){ | |
$filename = array_pop($path); | |
} | |
foreach($path as $dirname){ | |
if(!file_exists($dir.$dirname)){ | |
$ok = mkdir($dir.$dirname); | |
if ($ok===false){ | |
errors('code 206 mkdir'.$dir.$dirname); | |
return false; | |
} | |
} | |
$dir= $dir.$dirname.SLASH; | |
//var_dump($dir); | |
} | |
return true; | |
} | |
/** | |
* $array_files : array('uploads\file.jpg','uploads\file2.jpg') | |
* $dest : 'temp\user01\' | |
*/ | |
function copy_files_w_folders($array_files,$dest){ | |
foreach ($array_files as $path){ | |
create_dir($dest.$path,0,1); | |
copy(DIR.$path,DIR.$dest.$path); | |
} | |
} | |
function copy_files($array_files,$dest){ | |
create_dir($dest) ; | |
foreach ($array_files as $path){ | |
$pos = strrpos($path, SLASH); | |
$fileName= substr($path, $pos); | |
if(file_exists(DIR.$dest.$fileName)){ | |
//split fileName into name and suffix | |
$ext = strrpos($fileName, '.'); | |
$fileName_a = substr($fileName, 0, $ext); | |
$fileName_b = substr($fileName, $ext); | |
// if no file extension | |
if ($ext===false){ | |
$fileName_a = $fileName_b; | |
$fileName_b = '.unknown'; | |
$fileName = $fileName_a.$fileName_b; | |
} | |
$count = 1; | |
while (file_exists(DIR.$dest . $fileName_a . '_' . $count . $fileName_b)) | |
$count++; | |
$fileName = $fileName_a . '_' . $count . $fileName_b; | |
} | |
copy(DIR.$path,DIR.$dest.$fileName); | |
} | |
} | |
function Zip($source, $destination) | |
{ | |
$array_files=get_all_files(''); | |
$count=count($array_files); | |
echo $count.' Files'; | |
$step=ceil($count/100); | |
$j=0; | |
echo '<br />'; | |
for ($i = 1; $i <= 76; $i++) { | |
echo ' '; | |
} | |
echo '->|'; | |
echo 0; | |
if (!extension_loaded('zip') || !file_exists($source)) { | |
echo 1; | |
return false; | |
} | |
$zip = new ZipArchive(); | |
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { | |
echo 2; | |
return false; | |
} | |
dump($destination); | |
echo 3; | |
$source = str_replace('\\', '/', realpath($source)); | |
if (is_dir($source) === true) | |
{ | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) | |
{ | |
$file = str_replace('\\', '/', realpath($file)); | |
if (is_dir($file) === true) | |
{ | |
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
} | |
else if (is_file($file) === true) | |
{ | |
$zip->addFile($file,str_replace($source . '/', '', $file)); | |
} | |
$j++; | |
if($j>=$step){ | |
$j=0; | |
echo '|'; | |
} | |
} | |
} | |
else if (is_file($source) === true) | |
{ | |
echo 6; | |
$zip->addFile($source,basename($source)); | |
} | |
$ok= $zip->close(); | |
dump($zip->getStatusString(),$ok); | |
return $ok; | |
} | |
function get_all_files_r($dir,$reset = 0){ | |
static $files = array(); | |
static $i = 0; | |
if ($reset == 1){ | |
$files = array(); | |
$i=0; | |
} | |
$i++; | |
if($i>50000){ | |
errors('code 208 endlosschlaufe'); | |
return false; | |
} | |
if (!is_dir(DIR.$dir)){ | |
errors('code 207 dir'); | |
} | |
$scan = scandir(DIR.$dir); | |
array_shift($scan); | |
array_shift($scan); | |
$dirslash = (empty($dir))?'':$dir.SLASH; | |
foreach ($scan as $element){ | |
if (is_dir(DIR.$dirslash.$element)){ | |
get_all_files_r($dirslash.$element); | |
} | |
else { | |
array_push($files,$dirslash.$element); | |
} | |
} | |
return $files; | |
} | |
function dump($var,$var2 ='random string test test' ){ | |
echo '<pre>'; | |
if($var2==='random string test test'){ | |
var_dump($var); | |
} | |
else{ | |
var_dump($var,$var2); | |
} | |
echo '</pre>'; | |
} | |
if (!isset($errors)){ | |
$errors = '' ; | |
} | |
function errors($errorstring){ | |
global $errors; | |
$errors .= $errorstring.'<br />'; | |
} | |
echo $errors; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment