PHP文件:
这是我在php中的表单操作文件 我成功上传图片,但文本文件产生“无效文件错误” 可能是什么错误以及如何解决它?
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
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")
|| ($_FILES["file"]["type"] == "text/txt"))
&& ($_FILES["file"]["size"] < 20000000)
&& 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("C:/inetpub/wwwroot/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"C:/inetpub/wwwroot/" . $_FILES["file"]["name"]);
echo "Stored in: " . "C:/inetpub/wwwroot/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
提前致谢。
答案 0 :(得分:5)
我认为.text文件的mime类型不正确。您可以在IF语句中尝试txt/plain
而不是txt/text
。
答案 1 :(得分:1)
使用以下代码:
|| ($_FILES["file"]["type"] == "text/plain"
而不是
|| ($_FILES["file"]["type"] == "text/txt"
的列表