ZIP: Include parent directory (PHP) -


i need help, have code zipping folder, problem not include target or parent folder in zip file.

  test     -- 1     -- 2 

only 1 , 2 in zip file, want include test directory.

this code

$source='/testing'; $destination='/test';  $img_dir = array("1", "2"); $arrlength = count($img_dir);      foreach ($img_dir $k => $v) {     zipdata($destination.'/'.$v, $destination.'/img_'.$v.'.zip'); }  function zipdata($source, $destination) {     if (extension_loaded('zip')) {         if (file_exists($source)) {             $zip = new ziparchive();             if ($zip->open($destination, ziparchive::create)) {                 $source = realpath($source);                 if (is_dir($source)) {                     $files = new recursiveiteratoriterator(new recursivedirectoryiterator($source), recursiveiteratoriterator::self_first);                     foreach ($files $file) {                         $file = realpath($file);                         if (is_dir($file)) {                             $zip->addemptydir(str_replace($source . '/', '', $file . '/'));                         } else if (is_file($file)) {                             $zip->addfromstring(str_replace($source . '/', '', $file), file_get_contents($file));                         }                     }                 } else if (is_file($source)) {                     $zip->addfromstring(basename($source), file_get_contents($source));                 }             }             return $zip->close();         }     }     return false; } 

the best way add whole directory tree archive use recursion.

you have nice example in php manual, you're trying write yourself: http://php.net/manual/en/class.ziparchive.php. should check manual first.

it's in first user comment:

zip folder (include itself).  usage:    hzip::zipdir('/path/to/sourcedir', '/path/to/out.zip');   <?php  class hzip  {    /**     * add files , sub-directories in folder zip file.     * @param string $folder     * @param ziparchive $zipfile     * @param int $exclusivelength number of text exclusived file path.     */    private static function foldertozip($folder, &$zipfile, $exclusivelength) {      $handle = opendir($folder);      while (false !== $f = readdir($handle)) {        if ($f != '.' && $f != '..') {          $filepath = "$folder/$f";          // remove prefix file path before add zip.          $localpath = substr($filepath, $exclusivelength);          if (is_file($filepath)) {            $zipfile->addfile($filepath, $localpath);          } elseif (is_dir($filepath)) {            // add sub-directory.            $zipfile->addemptydir($localpath);            self::foldertozip($filepath, $zipfile, $exclusivelength);          }        }      }      closedir($handle);    }     /**     * zip folder (include itself).     * usage:     *   hzip::zipdir('/path/to/sourcedir', '/path/to/out.zip');     *     * @param string $sourcepath path of directory zip.     * @param string $outzippath path of output zip file.     */    public static function zipdir($sourcepath, $outzippath)    {      $pathinfo = pathinfo($sourcepath);      $parentpath = $pathinfo['dirname'];      $dirname = $pathinfo['basename'];       $z = new ziparchive();      $z->open($outzippath, ziparchive::create);      $z->addemptydir($dirname);      self::foldertozip($sourcepath, $z, strlen("$parentpath/"));      $z->close();    }  }  ?> 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -