drkxgs9358 2019-08-05 14:06
浏览 216

PHP ZipArchive :: close返回false

I'm trying to change some docx variables from PHP. But on save step always got false.

I tried to 777 permissions to directory, php-zip extension, "ext-zip": "*" in composer.json.

My code:

$zip = new \ZipArchive();

        $wordDoc = $this->container->get('kernel')->getRootDir() . '/../file.docx';
        $fileToModify = 'word/document.xml';

        if ($zip->open($wordDoc, ZipArchive::CREATE) === TRUE) {
            $oldContents = $zip->getFromName($fileToModify);

            $newContents = str_replace(' MERGEFIELD __FIRSTNAME__ ', 'NEWNAME', $oldContents);

            $zip->deleteName($fileToModify);

            $zip->addFromString($fileToModify, $newContents);

            if ($zip->close()) {
                echo "Done.";
            }
            else {
                echo 'fail';exit();
            }
        }

php -m zip enabled:

zip,
zlib

php --ini zip enabled:

/etc/php7/conf.d/00_zip.ini,

I expect overwrite old file and get new with new values but always got false on $zip->close();

  • 写回答

1条回答 默认 最新

  • dos49618 2019-08-05 15:37
    关注

    Solved. Before working needs to create a copy of origin file.

    $original = $this->container->get('kernel')->getRootDir() . '/../file.docx';
    if (copy($original, $this->container->get('kernel')->getRootDir() . '/../temp/newFile.docx')) {
          $tempFile = $this->container->get('kernel')->getRootDir() . '/../temp/newFile.docx';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败