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