doudou348131346 2016-12-06 05:07
浏览 24
已采纳

PHP - 在zip存档中编辑文件,并在关闭之前另存为另一个存档名称

I have a Microsoft Word file that I am using as a template. (testingsample.docx) The plan is to use form input values to create a lease agreement. The code I found below works very well for opening up the .docx file and finding and replacing the required strings. The problem is it only works once. The first time it is ran, it overwrites my template. I am trying to find a way to open testingsample.docx, make the required string changes, and save the archive as testingsamplecopy.docx without altering testingsample.docx.

Thanks in advance for any help!

// Create the Object.
$zip = new ZipArchive();

$inputFilename = 'testingsample.docx';

// Open the Microsoft Word .docx file as if it were a zip file... because it is.
if ($zip->open($inputFilename, ZipArchive::CREATE)!==TRUE) {
    echo "Cannot open $inputFilename :( "; die;
}

// Fetch the document.xml file from the word subdirectory in the archive.
$xml = $zip->getFromName('word/document.xml');

// Replace the strings
$xml = str_replace("1111","Tenant Name Here",$xml);
$xml = str_replace("2222","Address Here",$xml);

// Write back to the document and close the object
if ($zip->addFromString('word/document.xml', $xml)) { echo 'File written!'; }
else { echo 'File not written.  Go back and add write permissions to this folder!l'; }

$zip->close();

header("Location: testingsample.docx");

?>
  • 写回答

1条回答 默认 最新

  • douliang9057 2016-12-06 06:17
    关注

    Just copy from the template file to the target file, then open the target file and not the template.

    Also, I changed the code at the header line to use the variable of the name of the file and not a static one.

    <?php
    
    // Create the Object.
    $zip = new ZipArchive();
    
    $templateFilename = 'testingsample.docx';
    $inputFilename = 'testingsamplecopy.docx';
    
    if(!copy($templateFilename, $inputFilename)) {
        die("Could not copy '$templateFilename' to '$inputFilename');
    }
    
    // Open the Microsoft Word .docx file as if it were a zip file... because it is.
    if ($zip->open($inputFilename, ZipArchive::CREATE)!==TRUE) {
        echo "Cannot open $inputFilename :( "; die;
    }
    
    // Fetch the document.xml file from the word subdirectory in the archive.
    $xml = $zip->getFromName('word/document.xml');
    
    // Replace the strings
    $xml = str_replace("1111","Tenant Name Here",$xml);
    $xml = str_replace("2222","Address Here",$xml);
    
    // Write back to the document and close the object
    if ($zip->addFromString('word/document.xml', $xml)) { echo 'File written!'; }
    else { echo 'File not written.  Go back and add write permissions to this folder!l'; }
    
    $zip->close();
    
    // I also chaned this to use your variable instead of a static value.
    header("Location: $inputFilename");
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题