1

だからここに私のコードがあります:

<?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」という文字列は、ドキュメントを開くとドキュメントに含まれています)。

私の Lnux 本番サーバーでは機能しません (「$FIRST_AND_LAST_NAME」文字列はまだ存在しますが、「John Doe」はありません)。

エラーや通知はありません。「ok」はエラーなしで出力されます。test.docx ファイルの権限が 777 に設定されていることを確認しました。

4

3 に答える 3

1

close()false を返す場合は、アーカイブの書き込み中にエラーが発生しました。

getStatusString正確なエラー メッセージを取得するために使用します。

于 2010-08-25T14:16:54.603 に答える
1

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

私のUbuntu 12.04で動作します

docx ファイルを作成すると同時に変数を入力することを忘れないでください。つまり、入力しないFIRST_AND_LAST_NAMEで、その後にシンボルを追加します$。さまざまな XML コードを作成します。

于 2012-10-18T04:52:19.917 に答える
0

OK、phpclasses で見つけたクラスを使用しました。

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

作業コードは次のとおりです。

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

    }
}
于 2010-08-26T08:16:01.480 に答える