如何在php中上传doc和docx?

时间:2014-01-09 06:32:05

标签: php upload

我试图通过html输入在php上传文档和docx,但我无法上传doc和docx,即使我正确提到mime类型。请帮助我上传doc和docx.i上面写了我的代码..谢谢你提前..

<?php

$allowedExts = array(".doc", ".docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
 }
 }
else
{
echo "Invalid file";
 }

5 个答案:

答案 0 :(得分:2)

2个修正:

1。更改扩展数组

$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");

2。允许docx将doubled行更改为

|| ($_FILES["file"]["type"] 
    == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")

答案 1 :(得分:1)

这是正确的代码

$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
 }
 }
else
{
echo "Invalid file";
 }

答案 2 :(得分:0)

.doc

不需要docx
$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");

答案 3 :(得分:0)

试试这个:

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

}

答案 4 :(得分:0)

  $docx = $_FILES['file']['name'];
  $docxTmp = $_FILES['file']['tmp_name'];
  $docxSize = $_FILES['file']['size'];
  $type = $_FILES["file"]["type"];
  if ($docx) {
    $file_dir = '../upload/';
    $allowedExts = array("doc", "docx");
    if (($type == "application/msword" || $type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && in_array($extension, $allowedExts)) {
      move_uploaded_file($docxTmp, $file_dir . $docx);
    }
  }