php - zip main folder with sub folder inside -
i have folder files , sub folder inside. how im going read directory , zip main folder?
ex:
maindirectory --- file 1 --- file 2 --- subdirectory 1 ------ file 3 ------ file 4 --- subdirectory 2 ------ file 5 ------ file 6
i'm using script:
function zip($source, $destination, $include_dir = false) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } if (file_exists($destination)) { unlink ($destination); } $zip = new ziparchive(); if (!$zip->open($destination, ziparchive::create)) { return false; } $source = str_replace('\\', '/', realpath($source)); if (is_dir($source) === true) { $files = new recursiveiteratoriterator(new recursivedirectoryiterator($source), recursiveiteratoriterator::self_child); if ($include_dir) { $arr = explode("/",$source); $maindir = $arr[count($arr)- 1]; $source = ""; ($i=0; $i < count($arr) - 1; $i++) { $source .= '/' . $arr[$i]; } $source = substr($source, 1); $zip->addemptydir($maindir); } foreach ($files $file) { $file = str_replace('\\', '/', $file); // ignore "." , ".." folders if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) continue; $file = realpath($file); if (is_dir($file) === true) { $zip->addemptydir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addfromstring(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addfromstring(basename($source), file_get_contents($source)); } return $zip->close(); }
and call function this:
zip('image/data/','aaa.zip',false);
but zip whole c:
folder. want zip document inside image/data/
folder.
how can format correct directory , it's subdirectories?
try this.
zipfile('image/data/','aaa.zip', true); /** * function zipfile. creates zip file source destination * * @param string $source source path zip * @param string $destination destination path zip * @param string|boolean $flag optional if true includes folder * @return boolean */ function zipfile($source, $destination, $flag = '') { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ziparchive(); if (!$zip->open($destination, ziparchive::create)) { return false; } $source = str_replace('\\', '/', realpath($source)); if($flag) { $flag = basename($source) . '/'; //$zip->addemptydir(basename($source) . '/'); } if (is_dir($source) === true) { $files = new recursiveiteratoriterator(new recursivedirectoryiterator($source), recursiveiteratoriterator::self_first); foreach ($files $file) { $file = str_replace('\\', '/', realpath($file)); if (is_dir($file) === true) { $zip->addemptydir(str_replace($source . '/', '', $flag.$file . '/')); } else if (is_file($file) === true) { $zip->addfromstring(str_replace($source . '/', '', $flag.$file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addfromstring($flag.basename($source), file_get_contents($source)); } return $zip->close(); }
Comments
Post a Comment