PHP 上传表格、PDF、Doc &文档

2022-01-09 00:00:00 upload php html

我正在努力使这个上传代码适用于 docx 文件,它适用于 doc 和 pdf..

I'm struggling to make this upload code work for a docx file, it works okay for doc and 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>";
 }

这是不久前的一个项目的一部分,老实说,我不记得该怎么做..

this is part of a project from a while ago and i honestly can't remember how to do it..

我知道这不是最安全的上传方法,但如果有人可以提供帮助,我们将不胜感激!

I know it's not the most secure upload method, but if someone could help it would be appreciated!

我想我需要在这里添加另一行:

I'm thinking i need to add another line here:

if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)

只是不确定是什么......感谢帮助!

Just not sure what.. Help is appreciated!

所以我已经到了这个阶段(在评论的帮助下!)

So i've got to this stage (with the help of comments!)

$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

But now its coming up with: Parse error: syntax error, unexpected T_BOOLEAN_AND in /var/sites/s/stanation.com/public_html/forms/process/insert.php on line 30

推荐答案

对于 docx 检查这个 MIME 类型

For docx check this MIME type

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";
      }
  }

相关文章