将ZipArchive与wordpress wp_filesystem一起使用

时间:2018-09-03 16:36:17

标签: php wordpress plugins

我正在编写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();

0 个答案:

没有答案