保存已编辑的 zip 存档 (docx) 时出现问题

发布于 2024-09-16 13:20:56 字数 655 浏览 5 评论 0原文

这是我的代码:

<?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。

So here is my code:

<?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';
}

Its purpose is simple. It opens a test.docx file, searches for all occurences of a string "$FIRST_AND_LAST_NAME" and replaces them with "John Doe".

It works perfectly on my Windows development server (the "John Doe" string is in the docuemnt when I open it).

It does not work on my Lunux production server ("$FIRST_AND_LAST_NAME" string is still there, there is no "John Doe").

There is no error or notice, the "ok" is printed without any errors. I made sure the test.docx file has priviledges set to 777.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花心好男孩 2024-09-23 13:20:57

如果 close() 返回 false,则说明写出存档时出错。

使用 getStatusString 获取确切的错误消息。

If close() returns false, there was an error writing out the archive.

Use getStatusString to get the exact error message.

比忠 2024-09-23 13:20:57

$zip->addFromString('word/document.xml', $xmlString); 之前添加 sleep(1)

它适用于我的 Ubuntu 12.04

不要忘记创建 docx 文件时同时输入变量,我的意思是永远不要输入 FIRST_AND_LAST_NAME 然后在后面添加符号 $ 。它创建不同的 XML 代码。

Add sleep(1) before $zip->addFromString('word/document.xml', $xmlString);

It works on my Ubuntu 12.04

Don't forget to type your variable in same time when you create a docx file, I mean never type FIRST_AND_LAST_NAME and then you add a symbol $ after that. It creates different XML code.

哀由 2024-09-23 13:20:57

好的,我使用了在 phpclasses 中找到的类:

http://phpclasses.web4u.cz/package/6278-PHP-Edit-a-Zip-archive-in-pure-PHP-no-temporary-files.html< /a>

这是工作代码:

private function GenerateDocx($theTemplate, array $theReplacemenArray, $theOutputFile)
{
    $aSearchArray = array();
    foreach(range('A','Z') as $aLetter) {
        $aSearchArray[] = str_repeat($aLetter, 5);
    }
    $aArrayCountDifference = count($aSearchArray) - count($theReplacemenArray);
    $aSearchArray = array_slice($aSearchArray, 0, -$aArrayCountDifference);     

    require_once('tbszip.php');
    $tbszip = new clsTbsZip();
    $tbszip->Open($theTemplate);

    $aXmlPath = 'word/document.xml';

    if (true === $tbszip->FileExists($aXmlPath)) {

        $aXmlString = $tbszip->FileRead($aXmlPath);

        $aXmlString = str_replace($aSearchArray, $theReplacemenArray, $aXmlString);

        if (1 != $tbszip->FileReplace($aXmlPath, $aXmlString)) {
            throw new Exception('FileReplace() failed.');
        }

        $tbszip->Flush(TBSZIP_FILE, $theOutputFile);

        $tbszip->Close();

    }
}

Ok, I have used a class I found at phpclasses:

http://phpclasses.web4u.cz/package/6278-PHP-Edit-a-Zip-archive-in-pure-PHP-no-temporary-files.html

Here is the working code:

private function GenerateDocx($theTemplate, array $theReplacemenArray, $theOutputFile)
{
    $aSearchArray = array();
    foreach(range('A','Z') as $aLetter) {
        $aSearchArray[] = str_repeat($aLetter, 5);
    }
    $aArrayCountDifference = count($aSearchArray) - count($theReplacemenArray);
    $aSearchArray = array_slice($aSearchArray, 0, -$aArrayCountDifference);     

    require_once('tbszip.php');
    $tbszip = new clsTbsZip();
    $tbszip->Open($theTemplate);

    $aXmlPath = 'word/document.xml';

    if (true === $tbszip->FileExists($aXmlPath)) {

        $aXmlString = $tbszip->FileRead($aXmlPath);

        $aXmlString = str_replace($aSearchArray, $theReplacemenArray, $aXmlString);

        if (1 != $tbszip->FileReplace($aXmlPath, $aXmlString)) {
            throw new Exception('FileReplace() failed.');
        }

        $tbszip->Flush(TBSZIP_FILE, $theOutputFile);

        $tbszip->Close();

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文