我正在努力使这个上传代码适用于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>";
}
Run Code Online (Sandbox Code Playgroud)
这是前一个项目的一部分,老实说我不记得怎么做了..
我知道这不是最安全的上传方法,但如果有人可以提供帮助,我们将不胜感激!
我想我需要在这里添加另一行:
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
Run Code Online (Sandbox Code Playgroud)
只是不确定是什么..帮助表示赞赏!
编辑:所以我已经到了这个阶段(在评论的帮助下!)
$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
{
Run Code Online (Sandbox Code Playgroud)
但现在它出现了:解析错误:语法错误,第30行/var/sites/s/stanation.com/public_html/forms/process/insert.php中的意外T_BOOLEAN_AND
Sha*_*ran 12
对于docx检查此MIME类型
application/vnd.openxmlformats-officedocument.wordprocessingml.document
Run Code Online (Sandbox Code Playgroud)
编辑:
这是代码.你缺少括号
<?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";
}
}
Run Code Online (Sandbox Code Playgroud)