如何才允许上传.doc文件?

时间:2013-03-25 09:05:29

标签: php upload doc

这是我的代码,但是当我尝试上传doc文件时,响应是“无效文件”.....非常感谢。顺便说一句,我的第二个问题涉及将上传文件重命名为所需格式“实际时间+原始标题”$ date。

<?php
$datum = Date("j/m/Y/H/i/s", Time());
echo($date);

$allowedExts = array("doc");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/msword"))
&& ($_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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("uploaded_papers/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "uploaded_papers/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "uploaded_papers/" . $_FILES["file"]["name"];
  }
  }
  }                                          
  else
  {
  echo "Invalid file";
  }
  ?> 

3 个答案:

答案 0 :(得分:0)

改变这个:

$extension = end(explode(".", $_FILES["file"]["name"]));

要:

$extension = array_pop(explode(".", $_FILES["file"]["name"]));

Array_pop给出数组中的最后一个元素。

关于Word文档的正确MIME类型:http://filext.com/faq/office_mime_types.php

.doc
application/msword
.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document

所以我会选择这样的事情:

$allowedMimes = array( "application/msword" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" , "application/vnd.ms-word");

if ($_FILES["file"]["size"] < 2000000
&& in_array($_FILES["file"]["type"], $allowedMimes))

要查看上传文件的MIME类型,只需echo $_FILES["file"]["type"];,看看它不是图片。请回复mimetype,以便我们可以看到“普通”文档是什么。 使用您自己的结果更新了allowedMimes数组。

答案 1 :(得分:0)

试试这个,看看这是否适合你

            <?php

        $datum = Date("j/m/Y/H/i/s", Time());
        echo($date);
        $allowedExts = array("doc","docx");
        $extension = array_pop(explode(".", $_FILES["file"]["name"]));

        if ((($_FILES["file"]["type"] == "application/msword"))
        && ($_FILES["file"]["size"] < 2000000)
        && in_array($extension, $allowedExts))
        {
        if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {    
            echo "hello";
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (file_exists("uploaded_papers/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
            //changed file name 
            $file_name = time()."_".$_FILES['file']['name'];
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" .$file_name);
          echo "Stored in: " . "uploaded_papers/" .$file_name;
          }
          }
          }                                          
          else
          {
          echo "Invalid file";
          }
          ?> 

更改

  1. 使用array_pop而不是end。
  2. 将要上传的文件名更改为currenttimestamp_orignalname.doc

答案 2 :(得分:-1)

你为什么不做像

这样的事情
$extension = explode(".", $_FILES["file"]["name"]);

if($extension[1]=='doc' || $extension[1]=='docx'){
  ///begin uploads
}else{
   ///fail message
}