PHP ZipArchive 无法破坏 zip 上下文

标签 php zip

在尝试压缩主机中文件夹的内容以更轻松地迁移数据(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();

有什么猜测吗?

最佳答案

我刚刚在自己的一个项目中解决了相同的错误消息...不知道是否是相同的问题,但就我而言,我没有创建要添加 zip 的文件夹,所以它会抛出该错误错误。添加文件夹后,一切正常。希望有帮助。

关于PHP ZipArchive 无法破坏 zip 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306946/

相关文章:

php - 如何将自定义字段添加到 IPB 中的表单

Php 和 Mysql 根据学生的总分对学生进行排名并给予 .如果他们是领带,也不会连续排名

java - ZipOutputStream 不写入文件内容

linux - 如果从 cron 运行,zip 会创建空文件

使用 zip 输入文件的 Hadoop 流式处理

python - 为什么解压后 Python 不能立即识别 CSV 的文件大小?

zip - 将 .gz 文件添加到 .zip 存档而不解压缩和重新压缩?

php - 在 'header.php'或 'function.php'文件中插入Bootstrap目录更好吗?

php - 在 laravel 上全局设置 lc_time_names

Javascript 结果需要在 php 中捕获到电子邮件