我想在用户上传后提取docx我要显示内容。但似乎我不知道如何调用它...因为它一直显示“FILE NOT FOUND”。 如果我定义它,它的显示效果很好:
$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);
}
?>
答案 0 :(得分:0)
$_FILES['uploaded_file']['tmp_name']
不包含文档的名称,它包含类似&#39; / tmp / asdjashdkjashda&#39;您必须使用:$_FILES["uploaded_file"]["name"]
才能提取扩展名。
您的代码无效,因为您要从&#39; / tmp / asdjashdkjashda&#39;中提取扩展程序。而且不是&#39; docx&#39;所以你总是在寻找$dataFile = "content.xml";
(只适用于odt文件)。
因此,要使用['name']
扩展邮件,请使用['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);