因此,我正在尝试将上传功能添加到仅允许某些文件类型的联系页面。这些文件应存储在“上传”文件夹的根目录中。但是,文件没有被上传,我在 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";
}
?>