PHP 上传表格、PDF、Doc &文档 [英] PHP Upload form, PDF, Doc & Docx

查看:16
本文介绍了PHP 上传表格、PDF、Doc &文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使这个上传代码适用于 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";
      }
  }

这篇关于PHP 上传表格、PDF、Doc &amp;文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆