所以这是我的代码:
<?php
$zip = new ZipArchive;
if ($zip->open('test.docx') === TRUE) {
$xmlString = $zip->getFromName('word/document.xml');
$xmlString = str_replace('$FIRST_AND_LAST_NAME', 'John Doe', $xmlString);
$zip->addFromString('word/document.xml', $xmlString);
echo 'ok';
$zip->close();
} else {
echo 'failed';
}
它的目的很简单。它打开一个 test.docx 文件,搜索所有出现的字符串“$FIRST_AND_LAST_NAME”,并将它们替换为“John Doe”。
它在我的 Windows 开发服务器上完美运行(当我打开它时,“John Doe”字符串在文档中)。
它在我的 Lunux 生产服务器上不起作用(“$FIRST_AND_LAST_NAME”字符串仍然存在,没有“John Doe”)。
没有错误或通知,“ok”打印没有任何错误。我确保 test.docx 文件的权限设置为 777。