尝试压缩主机中文件夹的内容以更轻松地迁移数据时(VPS,没有花哨的界面面板),脚本会抛出一个我无法解码的错误。我总是可以从命令行压缩内容并使用它,但我想创建一个简单的界面供我的客户端随时下载我的工作。这是错误:
Warning: Unknown: Cannot destroy the zip context in Unknown on line 0
剧本:
define('DS', DIRECTORY_SEPARATOR);
define('DR', realpath(dirname(__FILE__)).DS);
if (!isset($_GET['folder']))
{
die('folder missing');
}
$rootPath = DR.$_GET['folder'].DS;
// Initialize archive object
$zip = new ZipArchive();
$zip->open(DR.'backup'.DS.'backup.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
$zip->close();
任何猜测?
答案 0 :(得分:2)
我刚刚在我自己的一个项目中解决了相同的错误消息...不知道是否是同一个问题,但在我的情况下我没有创建zip被添加到的文件夹,所以它会抛出错误。一旦我添加了文件夹,一切正常。希望有所帮助。
答案 1 :(得分:1)
@ AlexW.H.B遇到相同的问题; ZipArchive的open方法可以打开包含不存在的文件夹的zip文件名,因此我尝试将任何文件创建为zip格式,但是php-fpm子进程被杀死,并且在尝试写入zip目的地时没有明确的错误消息。查看php-fpm日志后,错误在这里:
Warning: Unknown: Cannot destroy the zip context in Unknown on line 0
实际上,ZipArchive或php-fpm应该抛出一个带有明确错误消息的异常,该异常消息可以在PHP代码中捕获。