php - 将 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();
解决方案
推荐阅读
- python - Encrypt your entire program and decrypt only in-memory
- python - Add features to keras time series generator for LSTM
- freemarker - 如何从 cmsfn.link 的输出中删除 .html 扩展名?
- python - simpy处理抢占原因后如何恢复抢占进程
- javascript - Confusion about Flow error wrt Filter function
- typescript - 打字稿中有没有办法声明“嵌套对象内的字符串”之类的东西?
- node.js - 发送包含大量附件的多部分 gmail 电子邮件
- javascript - 从具有 ID 的嵌套文档中检索数据
- go - 将中间件附加到安装在路由器上的路由
- kotlin - 如何从启动器类启动 TornadoFX 应用程序