【问题标题】:"Invalid or unitialized Zip object" Error when closing DOCX file关闭 DOCX 文件时出现“无效或未初始化的 Zip 对象”错误
【发布时间】:2017-01-21 16:22:15
【问题描述】:

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

“警告:ZipArchive::close(): Zip 对象无效或未初始化”

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);
            $zip->close();
            $xml = new SimpleXMLElement($data);
            $pageCount = $xml->Pages;
        }
        $zip->close();
    }

    return $pageCount;
}

怎么了?

【问题讨论】:

    标签: php docx


    【解决方案1】:

    您只需在代码中关闭 2 个 zip,第二个将生成错误,因为您已经关闭了 zip。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      相关资源
      最近更新 更多