title: ZIP: Include parent directory (PHP)
body: <p>I need some help, I have code for zipping a folder, my problem is it does not include the target or parent folder in a zip file.</p>
  
  <pre><code>  Test
      -- 1
      -- 2
  </code></pre>
  
  <p>only 1 and 2 is in the zip file, I want to include the Test directory.</p>
  
  <p>This is my code</p>
  
  <pre><code>$source='/testing';
  $destination='/test';
  
  $img_dir = array("1", "2");
  $arrlength = count($img_dir);
  
  
     foreach ($img_dir as $k =&gt; $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-&gt;open($destination, ZIPARCHIVE::CREATE)) {
                  $source = realpath($source);
                  if (is_dir($source)) {
                      $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
                      foreach ($files as $file) {
                          $file = realpath($file);
                          if (is_dir($file)) {
                              $zip-&gt;addEmptyDir(str_replace($source . '/', '', $file . '/'));
                          } else if (is_file($file)) {
                              $zip-&gt;addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                          }
                      }
                  } else if (is_file($source)) {
                      $zip-&gt;addFromString(basename($source), file_get_contents($source));
                  }
              }
              return $zip-&gt;close();
          }
      }
      return false;
  }
  </code></pre>