title: Warning: ZipArchive::close(): Failure to create temporary file: Unknown error
body: <p>I have a strange error in PHP, where I traverse a lot of directories and archieve the old stuff by ZIP'ing the folder and then delete the folder with all its files inside. This works for the most part fine, but for a few entries I get this error:</p>
  <blockquote>
  <p>Warning: ZipArchive::close(): Failure to create temporary file:
  Unknown error in
  myfile.php on
  line 132</p>
  </blockquote>
  <p>I have this PHP code, taken directly from <a href="https://stackoverflow.com/a/4914807/2028935">https://stackoverflow.com/a/4914807/2028935</a> but I inserted an <code>exit()</code> to make sure to catch an error:</p>
  <pre><code>// Get real path for our folder
  $rootPath = realpath(&quot;/myfolder/pathX&quot;);
  
  // Initialize archive object
  $zip = new ZipArchive();
  $zip-&gt;open(&quot;/myfolder/myfile.zip&quot;, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  
  // Create recursive directory iterator
  /** @var SplFileInfo[] $files */
  $files = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($rootPath),
      RecursiveIteratorIterator::LEAVES_ONLY
  );
  
  foreach ($files as $name =&gt; $file) {
      // Skip directories (they would be added automatically)
      if (!$file-&gt;isDir()) {
          // Get real and relative path for current file
          $filePath = $file-&gt;getRealPath();
          $relativePath = substr($filePath, strlen($rootPath) + 1);
  
          // Add current file to archive
          $zip-&gt;addFile($filePath, $relativePath);
      }
  }
  
  // Zip archive will be created only after closing object
  if(!$zip-&gt;close()) {
      exit();
  }
  </code></pre>
  <p>The error is triggered on line <strong>132</strong> which is this line, <code>if(!$zip-&gt;close()) {</code></p>
  <p>I don't have folder or file permission problems, as the folder and files do get deleted (another code), but I cannot figure out what the <code>Unknown error</code> is in ZIP?</p>
  <p>I am using PHP 7.4 on a Windows Server.</p>
  <p><strong>### UPDATE ###</strong></p>
  <p>It seems the unknown error is due to the ZIP file path being too long, but I don't understand why. If the path in <code>$zip-&gt;open(&quot;/myfolder/myfile.zip&quot;, ZipArchive::CREATE | ZipArchive::OVERWRITE);</code> is longer than or equal to <strong>250</strong> characters, then it comes with this error!?</p>
  <p>I could understand if there would be a limit with 256 or 260 characters, as I can see there could be some limits there, but why 250?</p>
  <p>Currently I am investigating if I can reduce the path length by doing a local drive/folder mapping with the DOS command <code>SUBST</code> or through registry, <code>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices</code></p>