当我试图将主机文件夹的内容压缩以便更轻松地迁移数据(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();
任何猜测?