繁体   English   中英

关闭 DOCX 文件时出现“无效或未初始化的 Zip 对象”错误

[英]"Invalid or unitialized Zip object" Error when closing DOCX file

当我尝试使用此代码计算 .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;
}

怎么了?

您只需在代码中关闭 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM