![](/img/trans.png)
[英]Warning: ZipArchive::close(): Failure to create temporary file: Unknown error
[英]Why am I getting error "Failure to create temporary file" when trying to close a zip file?
您好,這是我第一次在這里發帖,我不確定它是如何工作的,但我在下面包含了我的代碼。 我將不勝感激一些幫助。
我無法關閉我的 zip 文件。 我創建的標記(打印語句)正在執行。 我只收到 $zip->close() 的錯誤。
作為參考,我有 PHP 7.4.24 版並且我使用的是 Mac。 當我的同事在他的 Windows 系統上運行它時,它執行了。
這是我的瀏覽器中顯示的錯誤:
警告:ZipArchive::close():創建臨時文件失敗:第 55 行 /Applications/XAMPP/xamppfiles/htdocs/nameOfMyFolder/folderzip.php 中的權限被拒絕
<?php
// name of directory (folder)
$pathdir = "/Applications/XAMPP/xamppfiles/htdocs/nameOfMyFile/";
//name of zip file to be created when zipped
$zipcreated = "archive.zip";
// new zip class
$zip = new ZipArchive;
if (extension_loaded("zip")){
echo "Zip extension is loaded";
}
//phpinfo();
// PHP Version 7.4.24
// Create a zip file and open it, check if it worked
if($zip -> open($zipcreated, ZipArchive::CREATE ) == TRUE) {
// Store the path into the variable
// opendir opens a directory handle
$dir = opendir($pathdir);
while($file = readdir($dir)) {
// is_file checks if specified file is a regular file
echo $pathdir.$file;
if(is_file($pathdir.$file)) {
$zip -> addFile($pathdir.$file, $file);
echo "File/s copied";
} else {
//echo "File not copied";
}
//echo "While executed";
}
echo "Out of while loop";
$zip->close();
//$zip -> ZipArchive::close();
//zip_close(resource ($zip));
//$zip -> getStatusString();
} else {
die ("Can't open $zipcreated");
}
?>
我已經重構了您的代碼,它現在似乎可以正常工作了。 請注意我是如何使用“ !
not”條件的,這樣代碼可以更快地退出並且不需要嵌套。
<?php
$folder_to_archive = "/path/to/your/folder/";
$zip_file = "archive2.zip";
if (!extension_loaded("zip"))
die("Zip extension could not be loaded" . PHP_EOL);
$zip = new ZipArchive;
if ($zip->open($zip_file, ZipArchive::CREATE) !== true)
die('Could not create Zip File' . PHP_EOL);
$dir = array_diff(scandir($folder_to_archive), ['.','..']);
foreach ($dir as $file) {
$full_filename = $folder_to_archive . $file;
if (!is_file($full_filename))
continue;
$zip->addFile($full_filename, $file);
}
$zip->close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.