我正在努力使此上传代码适用于docx文件,它适用于doc和pdf。
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
这是不久前一个项目的一部分,老实说,我不记得该怎么做。
我知道这不是最安全的上传方法,但如果有人能提供帮助,将不胜感激!
我想我需要在这里添加另一行:
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
只是不确定是什么..感谢帮助!
编辑:所以我到了这个阶段(在评论的帮助下!
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
//if ((($_FILES["file"]["type"] == "application/pdf")
//|| ($_FILES["file"]["type"] == "application/msword"))
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats- officedocument.wordprocessingml.document"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
但现在它提出了:解析错误:语法错误,第 30 行/var/sites/s/stanation.com/public_html/forms/process/insert.php 中的意外T_BOOLEAN_AND
对于docx
检查此 MIME 类型
application/vnd.openxmlformats-officedocument.wordprocessingml.document
编辑:
这是代码.您缺少括号
<?php
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Success";
}
}
以下检查将帮助您上传.docx
文件:
$_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
还有另一种方法可以完成任务。只需检查文件类型,然后您就可以进一步工作。
下面是用于检查文件类型的代码。
$target_dir = "uploads/";
$filename= $_FILES["fileupload"]["name"]; //gets filename with type
$target_file = $target_dir . basename($filename); //uploads/file.type
echo $target_file;
$extension= pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType = strtolower($extension);
if(strcmp($imageFileType,"docx")==0){
echo "Its word file";
}