我正在尝试构建一个简单的图像上传,但是当我尝试上传任何图像时,我收到一个错误,告诉我它是一个无效文件。谁能告诉我我做错了什么?
这是我的代码:
<?php
$allowExts = array("jpg", "jpeg", "gif", "png", "stl");
$extensions = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/stl"))
&& ($_FILES["file"]["size"] < 21000)
&& in_array($extension, $allowExts))
{
if ($_FILES ["file"]["error"] > 0){
echo "ERROR: " . $_FILES["file"]["error"] . "<br>";
}
else{
echo "Upload: " . $_FILES["file"]["name"];
echo "Type: " . $_FILES["file"]["type"];
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stores in: " . $_FILES["file"]["tmp_name"];
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";
}
?>
这是我根据我所做的编辑调整后的文件:
<?php
$allowExts = array("jpg", "jpeg", "gif", "png", "stl");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/stl"))
&& ($_FILES["file"]["size"] < 30000000)
&& in_array($extension, $allowExts))
{
if ($_FILES ["file"]["error"] > 0){
echo "ERROR: " . $_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 "Stores in: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("images/" . $_FILES["file"]["name"])){
echo $FILES["file"]["name"] . "already exists. ";
}
else{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $FILES["file"]["name"]);
echo "Stored in: " . "images/" . $FILES["file"]["name"];
}
}
}
else{
echo "Invalid File";
print_r($_FILES);
}
?>