0

我想在 php 中上传 pdf 或 doc 文件,我不想将它存储在数据库中,而是存储在文件系统中。但是当我尝试这样做时出现错误.....这是我的代码

if(isset($_POST['submit'])){

 $allowedExts = array("pdf", "doc","docx");
 $extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "'application/pdf")
|| ($_FILES["file"]["type"] == " application/x-pdf")
|| ($_FILES["file"]["type"] == "application/acrobat")
|| ($_FILES["file"]["type"] == "applications/vnd.pdf")
|| ($_FILES["file"]["type"] == "text/pdf")
|| ($_FILES["file"]["type"] == "text/x-pdf"))
&& ($_FILES["file"]["size"] < 20000)
&& 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("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";
 }

}
?> 

我不知道文件是否上传到哪里,但是当文件不是 pdf 或 doc 时它返回错误...请帮助?

当我对此进行编辑时,它工作正常

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
4

2 回答 2

0

当您上传 doc 或 pdf 时,它会返回错误,因为这是您允许文件具有的唯一扩展名。你在 $allowedExts = array("pdf", "doc","docx");

于 2014-01-12T15:29:06.477 回答
0

您是否将上传文件夹权限设置为 775 ?

另外,尝试:

$allowedExts = array("pdf", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")

使用MIME Types 玩弄它。

于 2013-06-19T18:16:24.520 回答