"无效或统一的Zip对象"关闭DOCX文件时出错

时间:2017-01-21 16:22:16

标签: php docx

当我尝试使用此代码计算.docx文件中的页面时,我总是收到错误:

  

"警告:ZipArchive :: close():"

中无效或统一的Zip对象
import re
s = "k0+k1+k1k2+k2k3+1+12"

re.sub(r"([a-zA-Z])(\d+)", r"\1[\2]", s)
# 'k[0]+k[1]+k[1]k[2]+k[2]k[3]+1+12'

出了什么问题?

1 个答案:

答案 0 :(得分:1)

您只需在代码中关闭2个拉链,第二个会在您关闭拉链时生成错误。

function PageCount_DOCX($file) {
    $pageCount = 0;

    $zip = new ZipArchive();

    if($zip->open($file) === true) {
        if(($index = $zip->locateName('docProps/app.xml')) !== false)  {
            $data = $zip->getFromIndex($index);
            // remove this one
            //$zip->close();
            $xml = new SimpleXMLElement($data);
            $pageCount = $xml->Pages;
        }
        // or remove this one
        $zip->close();
    }

    return $pageCount;
}
相关问题