phpzip

Warning: ZipArchive::close(): Failure to create temporary file: Unknown error


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:

Warning: ZipArchive::close(): Failure to create temporary file: Unknown error in myfile.php on line 132

I have this PHP code, taken directly from https://stackoverflow.com/a/4914807/2028935 but I inserted an exit() to make sure to catch an error:

// Get real path for our folder
$rootPath = realpath("/myfolder/pathX");

// Initialize archive object
$zip = new ZipArchive();
$zip->open("/myfolder/myfile.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
    // Skip directories (they would be added automatically)
    if (!$file->isDir()) {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
if(!$zip->close()) {
    exit();
}

The error is triggered on line 132 which is this line, if(!$zip->close()) {

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 Unknown error is in ZIP?

I am using PHP 7.4 on a Windows Server.

### UPDATE ###

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 $zip->open("/myfolder/myfile.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE); is longer than or equal to 250 characters, then it comes with this error!?

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?

Currently I am investigating if I can reduce the path length by doing a local drive/folder mapping with the DOS command SUBST or through registry, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices


Solution

  • After some more analysis I found that the Unknown error was due to the ZIP file path was too long - it was longer than 250 characters, which seems to be a Windows limitation, though I see fluctuating numbers on this limit, anything from 247 to 260.

    One possible solution I tried was doing a SUBST command in DOS command, which creates a drive for the long path - like this subst O: D:\very\long\..\path. This will give a O:\ where I can connect directly but this only works for my local user account nor does it survive a reboot.

    The solution that I found working, was adding a new REG_SZ to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices in the registry. I added this REG_SZ (string):

    O: => \DosDevices\D:\very\long...\path

    Insert this in to the registry

    After a reboot of the Windows Server (2019) I now have the O-drive, that I can use within IIS (and by then PHP) and my problem has been solved :-)