1

好吧,这是我的代码,但是当我尝试上传 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";
  }
  ?> 
4

3 回答 3

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
于 2013-03-25T09:40:06.787 回答
0

改变这个:

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

至:

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

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

关于 Word 文档的正确 MIME 类型:http: //fileext.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 数组。

于 2013-03-25T09:11:16.447 回答
-1

你为什么不做类似的事情

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

if($extension[1]=='doc' || $extension[1]=='docx'){
  ///begin uploads
}else{
   ///fail message
}
于 2013-03-25T09:39:35.363 回答