title: RecursiveIteratorIterator: include directory and php files in zip
body: <p>I have a script which ZIPS directories, but need to add *.php files also.</p>
  
  <p>Issue having is error is thrown when adding something like ../index.php.</p>
  
  <p>Error produced by script below is:</p>
  
  <pre><code>Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(../index.php): failed to open dir: Not a directory' in /home/mathtest/public_html/trig/admin/save.php:23 Stack trace: #0 /home/mathtest/public_html/trig/admin/save.php(23): RecursiveDirectoryIterator-&gt;__construct('../index.php') #1 {main} thrown in /home/mathtest/public_html/trig/admin/save.php on line 23
  </code></pre>
  
  <p>My script:</p>
  
  <pre><code>&lt;?php
  
  /* CONFIG */
  
  $pathToAssets = array("../images", "../Data", "../css", "../index.php");
  
  $filename = "temp/backup.zip";
  
  /* END CONFIG */
  
  
  $zip = new ZipArchive();
  
  $zip-&gt;open($filename, ZipArchive::CREATE);
  
  
  //add folder structure
  
  foreach ($pathToAssets as $thePath) {
  
      // Create recursive directory iterator
      $files = new RecursiveIteratorIterator(
              new RecursiveDirectoryIterator($thePath), RecursiveIteratorIterator::LEAVES_ONLY
      );
  
  
      foreach ($files as $name =&gt; $file) {
  
          if ($file-&gt;getFilename() != '.' &amp;&amp; $file-&gt;getFilename() != '..') {
  
              // Get real path for current file
              $filePath = $file-&gt;getRealPath();
  
              $temp = explode("/", $name);
  
              array_shift($temp);
  
              $newName = implode("/", $temp);
  
              // Add current file to archive
              $zip-&gt;addFile($filePath, $newName);
          }
      }
  }
  
  $zip-&gt;close();
  
  $yourfile = $filename;
  
  $file_name = basename($yourfile);
  
  header("Content-Type: application/zip");
  header("Content-Transfer-Encoding: Binary");
  header("Content-Disposition: attachment; filename=$file_name");
  header("Content-Length: " . filesize($yourfile));
  
  readfile($yourfile);
  
  unlink('temp/backup.zip');
  
  exit;
  ?&gt;
  </code></pre>
  
  <p>I have read about RecursiveIteratorIterator at <a href="http://php.net/manual/en/class.recursiveiteratoriterator.php" rel="nofollow">http://php.net/manual/en/class.recursiveiteratoriterator.php</a> and also many questions here without luck in solving.</p>
  
  <p>Replacing ../index.php with just ../ works, but that includes directories that do not want placed in zip.</p>
  
  <p>Any input to allow insertion of php in downloaded zip much appreciated.</p>