保存已编辑的zip存档(docx)时出现问题 [英] Problem saving edited zip archive (docx)

查看:66
本文介绍了保存已编辑的zip存档(docx)时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

<?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".

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".

它可以在我的Windows开发服务器上完美运行(打开文件时,"John Doe"字符串在文档中).

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

它在我的Lunux生产服务器上不起作用("$ FIRST_AND_LAST_NAME"字符串仍然存在,没有"John Doe").

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

没有错误或通知,"ok"的打印没有任何错误.我确保test.docx文件的权限设置为777.

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.

推荐答案

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

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

这是工作代码:

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

    }
}

这篇关于保存已编辑的zip存档(docx)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆