在提取docx文件之前找不到文件


File not found before extracting docx file

我想在用户上传后提取docx,我想显示内容。但我似乎不知道该怎么称呼它……因为它一直显示"文件未找到"。如果我定义:

$document = try.docx

所以我知道它不能调用上传的文件。这是源代码:

<?php
include 'configure.php';
if(isset($_FILES['uploaded_file'])) 
{
$document = $_FILES ['uploaded_file']['tmp_name'];// here was the issue.. tried many way but still failed
function extracttext($filename) 
{
    $ext = explode('.', $filename);
    $ext=end ($ext);
    if($ext == 'docx')
    $dataFile = "word/document.xml";
    else
    $dataFile = "content.xml";    
    $zip = new ZipArchive;
    if (true === $zip->open($filename)) 
    {
        if (($index = $zip->locateName($dataFile)) !== false) 
        {
            $text = $zip->getFromIndex($index);
            $xml = new DOMDocument;
            $xml->loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
            return strip_tags($xml->saveXML());
        }
        $zip->close();
    }
    return "File not found";
}
echo extracttext($document);
}
?>

$_FILES['uploaded_file']['tmp_name']不包含文档的名称,它包含类似'/tmp/asdjashdkjashda'的内容,您必须使用:$_FILES["uploaded_file"]["name"]来提取扩展名。

你的代码不工作,因为你是从'/tmp/asdjashdkjashda'提取扩展名,而不是'docx',所以你总是在寻找$dataFile = "content.xml";(只适用于odt文件)。

所以,要获得扩展使用['name'],打开zip使用['tmp_name']:

$document_path = $_FILES ['uploaded_file']['tmp_name'];
$document_name=$_FILES ['uploaded_file']['name'];
function extracttext($filename,$filepath){
    $ext = explode('.', $filename);
    [...]
    if (true === $zip->open($filepath)) 
    [...]
} 
echo extracttext($document_name,$document_path);