title: In PHP, what means this error : Warning: ZipArchive::close(): Read error: Bad file descriptor in (...)?
body: <p>I have a PHP script to make a zip with 2 functions : </p>
  
  <ol>
  <li>dirToArray : to get all files/empty_folders in an array</li>
  <li>create_zip : to call dirToArray() and make the zipArchive </li>
  </ol>
  
  <p>I got a weird warning which actually makes a real error because my zip archive is not built.</p>
  
  <p><strong>Warning result</strong></p>
  
  <blockquote>
    <p>Warning: ZipArchive::close(): Read error: Bad file descriptor in path/to/file.php on line x</p>
  </blockquote>
  
  <p>Someone can explain me what means : "Bad file descriptor" ?</p>
  
  <p>This is the code :</p>
  
  <p><strong>dirToArray</strong></p>
  
  <pre><code>/* to copy all file/folder names from a directory into an array*/
  function dirToArray($dir_path) {
      $result = array();
      $path = realpath($dir_path);
      $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
      foreach($objects as $name =&gt; $object) {
          if( $object-&gt;getFilename() !== "." &amp;&amp; $object-&gt;getFilename() !== "..") {
              $result[] = $object;
          }
      }
      return $result;
  }
  </code></pre>
  
  <p><strong>create_zip</strong></p>
  
  <pre><code>/* creates a compressed zip file */
  function create_zip($productPath = '', $dirName = '', $overwrite = false) {
      $fullProductPath = $productPath.$dirName;
      $a_filesFolders = dirToArray( $fullProductPath );
      var_dump($a_filesFolders);
      //if the zip file already exists and overwrite is false, return false
      $zip = new \ZipArchive();
      $zipProductPath =  $fullProductPath.'.zip';
      if($zip-&gt;open( $zipProductPath ) &amp;&amp; !$overwrite){
          $GLOBALS["errors"][] = "The directory {$zipProductPath} already exists and cannot be removed.";
      }
  
      //if files were passed in...
      if(is_array($a_filesFolders) &amp;&amp; count($a_filesFolders)){
          $opened = $zip-&gt;open( $zipProductPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE );
          if( $opened !== true ){
              $GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it.";
          }
  
          //cycle through each file
          foreach($a_filesFolders as $object) {
              //make sure the file exists
              $fileName = $object -&gt; getFilename();
              $pathName = $object -&gt; getPathname();
              if(file_exists($pathName)) {
                  $pos = strpos($zipProductPath , "/tmp/") + 5;
                  $fileDestination = substr($pathName, $pos);
                  echo $pathName.'&lt;br/&gt;';
                  echo $fileDestination.'&lt;br/&gt;';
                  $zip-&gt;addFile($pathName,$fileDestination);
              }
              else if(is_dir( $pathName )){
                  $pos = strpos($zipProductPath , "/tmp/") + 5;
                  $fileDestination = substr($pathName, $pos);
                  $zip-&gt;addEmptyDir($fileDestination);
              }else{
                  $GLOBALS["errors"][] = "the file ".$fileName." does not exist !";
              }
          }
  
          //close the zip -- done!
          $zip-&gt;close();
          //check to make sure the file exists
          return file_exists($zipProductPath);
      }else{
          return false;
      }
  }
  </code></pre>