php - PHP ZipArchive::close 返回 false
问题描述
我正在尝试从 PHP 更改一些 docx 变量。但是在保存步骤总是错误的。
我尝试在 composer.json 中对目录、php-zip 扩展名、“ext-zip”:“*”设置 777 权限。
我的代码:
$zip = new \ZipArchive();
$wordDoc = $this->container->get('kernel')->getRootDir() . '/../file.docx';
$fileToModify = 'word/document.xml';
if ($zip->open($wordDoc, ZipArchive::CREATE) === TRUE) {
$oldContents = $zip->getFromName($fileToModify);
$newContents = str_replace(' MERGEFIELD __FIRSTNAME__ ', 'NEWNAME', $oldContents);
$zip->deleteName($fileToModify);
$zip->addFromString($fileToModify, $newContents);
if ($zip->close()) {
echo "Done.";
}
else {
echo 'fail';exit();
}
}
启用 php -m zip:
zip,
zlib
php --ini zip 启用:
/etc/php7/conf.d/00_zip.ini,
我希望覆盖旧文件并使用新值获取新文件,但总是出错$zip->close();
解决方案
解决了。在工作之前需要创建原始文件的副本。
$original = $this->container->get('kernel')->getRootDir() . '/../file.docx';
if (copy($original, $this->container->get('kernel')->getRootDir() . '/../temp/newFile.docx')) {
$tempFile = $this->container->get('kernel')->getRootDir() . '/../temp/newFile.docx';
}
推荐阅读
- reactjs - Why does my NavLink's doesn't set to active using Semantic-ui-react?
- javascript - 如何使用 vuex 删除项目?
- debugging - 如何强制调试跟踪语句按顺序评估?
- gradle - 如何通过 LibGDX (Gradle) 导入和使用 JsInterop?
- c# - 在 SQL 查询中传递 DECLARE 参数
- node.js - ListItemText 不显示数据
- javascript - Javascript:如何从“动态”创建的复选框中获取值
- c++ - 如何实现 std::atomic 引用计数器?
- python - Python - 检查字符串中是否至少包含 3 个数字
- perl - 将文件从数字排序转换为字母排序