所以我正在尝试将上传功能添加到只允许某些文件类型的联系页面。这些文件应存储在“upload”文件夹的根目录中。但是,文件没有上传,我在PDF和docx文件上收到“无效文件”错误(如编程),但无法弄清楚原因。任何人都可以提供任何见解吗?
<?php
if($_POST) {
if($_POST["token"]==true){
$allowedExts = array("pdf","docx", "doc");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/docx")
|| ($_FILES["file"]["type"] == "application/doc"))
&& ($_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"] / 6144) . " 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/" . rand().$extension);
echo "Stored in: " . "upload/" . "rand().$extension)";
}
}
}
else
{
echo "Invalid file";
}
?>
答案 0 :(得分:0)
docx文件的文件类型不是application/docx
而是application/vnd.openxmlformats-officedocument.wordprocessingml.document
,但您也可能将其视为application/zip
; doc文件的mime类型为application/msword
,而不是application/doc