【问题标题】:PHP refusing to extract docx as zip from Windows/TempPHP 拒绝从 Windows/Temp 中提取 docx 作为 zip
【发布时间】:2023-12-02 16:41:01
【问题描述】:
$dir = "temp/docx";

    $errors = array();
    $zip = new ZipArchive;

    if($zip->open($file_path) === false){
        $errors[] = 'Failed to open file';
    }

    if (empty($errors)) {
        $zip->extractTo($dir,"word/document.xml");
        $zip->close();
$files = scandir($dir);
print_r($files);

好的,基本上由于某种原因提取不起作用。在看到文件夹为空后,我决定做一个 scandir 来查看它们是否在 php 完成后被删除。没有什么。 $files 变量不输出任何内容(当然除了 .. 和 .)。

zip 实际上是一个 docx 文件,在明确检查错误后,php 似乎认为 zip_open 有效,但我不确定这是否只是误报。

我想知道这是否是因为这实际上是一个 docx 文件,我需要将其显式保存为服务器上的 zip 文件。或者可能是因为这在上传后直接发生并且临时文件在能够对其执行任何操作之前被删除(我认为这不太可能,因为其他格式也可以正常工作)。也许我的两个假设都不接近,或者我有可能把整件事都写错了。有什么帮助吗?

【问题讨论】:

    标签: php upload zip extract docx


    【解决方案1】:

    给你:

    <?php
    
    /*Name of the document file*/
    $document = 'demo.docx';
    
    /*Directory*/
    $dir = "temp/docx/";
    
    /**Function to extract text*/
    function extracttext($filename, $action) {
        //Check for extension
        $ext = end(explode('.', $filename));
    
        //Check if DOCX file
        if($ext == 'docx'){
            $dataFile = "word/document.xml";
        //else it's probebly an ODT file
        } else {
            $dataFile = "content.xml";    
        }
    
        //Create a new ZIP archive object
        $zip = new ZipArchive;
    
        // Open the archive file
        if (true === $zip->open($filename)) {
            // If successful, search for the data file in the archive
            if (($index = $zip->locateName($dataFile)) !== false) {
                // Index found! Now read it to a string
                $text = $zip->getFromIndex($index);
                // Load XML from a string
                // Ignore errors and warnings
                $xml = DOMDocument::loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
                if($action == "save"){
                    // Save xml to file
                    file_put_contents($dir ."word/document.xml", $xml->saveXML());
                    return "File succesfully saved.";
                } else if($action == "text"){
                    // Remove XML formatting tags and return the text
                    return strip_tags($xml->saveXML());
                }
            }
            //Close the archive file
            $zip->close();
        }
    
        // In case of failure return a message
        return "File not found";
    }
    
    //Save xml file
    echo extracttext($document, "save");
    //Echo text from file
    echo extracttext($document, "text");
    
    ?>
    

    【讨论】:

    • 非常感谢老兄。我已经在上传页面上进行了扩展检查(每种单独的格式都有自己的包含)。您是否有机会向我深入解释一下为什么我的版本不起作用?我不喜欢只是复制和粘贴我喜欢理解和学习的代码。再次非常感谢。
    • @joopjoopjoop 好吧,当您意识到这一点时,您会大笑起来。您只是忘记在提取文件之前打开文件:P您只是打开它来查看是否可以打开它xD
    • 现在我得到“找不到文件”。文件名保存为 $file_path 但我添加了 $filename = $file_path 希望它可以这样工作。还有什么我需要做的吗对此进行更改以使其正常工作?
    • 哦,伙计,当我查看我的代码并看到我只检查 if(..=== false) lmao 时,我以为这就是你的意思,对吧?
    • 好吧,我只是添加了 $document 作为示例。你可以简单地做: echo extracttext($file_path, "save");如果 $file_path 持有像“mydoc.docx”这样的文档。如果它只保存“mydoc”,则将其更改为 extracttext($file_path .".docx", "save");
    最近更新 更多