首页 > 解决方案 > 将 ZipArchive 与 wordpress wp_filesystem 一起使用

问题描述

我正在编写 wordpress 插件,它需要使用 ziparchive 和 wordpress 文件系统 API 创建上传的 zip 文件,我尝试直接创建 zip 文件,但它没有获得正确的权限。我没有看到任何错误,但创建的 zip 始终具有其他用户读取的权限,这导致我在共享主机上出现问题。

//dir will be passed to the function
$dir = $wp_filesystem->find_folder($dir);
$fileName = $dir . "example.zip"
$wp_filesystem->put_contents($backup_path,$zip->open($fileName, ZipArchive::CREATE | ZipArchive::OVERWRITE), FS_CHMOD_FILE);

$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);
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();

标签: phpwordpressplugins

解决方案


推荐阅读