Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP拒绝从Windows/Temp中将docx提取为zip_Php_Upload_Zip_Extract_Docx - Fatal编程技术网

PHP拒绝从Windows/Temp中将docx提取为zip

PHP拒绝从Windows/Temp中将docx提取为zip,php,upload,zip,extract,docx,Php,Upload,Zip,Extract,Docx,好吧,基本上由于某种原因,提取不起作用。在看到文件夹是空的之后,我决定进行一次扫描,看看它们是否在php完成后被删除。没什么$文件变量输出除..之外的任何内容。。和 zip实际上是一个docx文件,在明确检查错误后,php似乎认为zip_open可以工作,但我不确定这是否只是一个误报 我想知道这是否是因为这实际上是一个docx文件,我需要显式地将其保存为服务器上的zip文件。或者可能是因为这是在上传之后直接发生的,临时文件在能够处理它之前就被删除了,我认为这是不可能的,因为其他格式工作正常。也许

好吧,基本上由于某种原因,提取不起作用。在看到文件夹是空的之后,我决定进行一次扫描,看看它们是否在php完成后被删除。没什么$文件变量输出除..之外的任何内容。。和

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

我想知道这是否是因为这实际上是一个docx文件,我需要显式地将其保存为服务器上的zip文件。或者可能是因为这是在上传之后直接发生的,临时文件在能够处理它之前就被删除了,我认为这是不可能的,因为其他格式工作正常。也许我的两个假设都不靠谱,或者我可能把整件事都写错了。有什么帮助吗?

给你:

$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);

谢谢你,伙计。我已经在上传页面上进行了扩展检查,每个单独的格式都有自己的include。你能不能给我解释一下为什么我的版本不起作用?我不喜欢只是复制和粘贴代码,我喜欢理解和学习。再次非常感谢。@joopjoop哦,当你意识到这一点时,你会笑得很厉害的。您只是忘记了在提取文件之前打开它:P您打开它只是为了看看是否可以打开它。现在我得到了“找不到文件”。文件名保存为$file\u path,但我添加了$filename=$file\u path,希望它能这样工作。还有什么我需要改变的吗?哦,伙计,我想是这样的,当我查看我的代码时,发现我只是在检查if..==false lmao,这就是你的意思,对吧?我只是添加了$document作为示例。您只需执行以下操作:echo extracttext$file\u path,save;如果$file\u路径包含类似mydoc.docx的文档。如果它只保存mydoc,则将其更改为extracttext$file\u path..docx,save;
<?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");

?>