Problem saving edited zip archive (docx)

六眼飞鱼酱① 提交于 2019-12-02 06:25:19

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

Use getStatusString to get the exact error message.

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.

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();

    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!