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");
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题