PHP 上传表单给出错误


PHP Upload form giving out errors

我是一个完全的PHP新手,我昨天了解了数组,我的老板让我写一个PHP表单,我在W3学校找到了一个,最后得到了

<html>
<body>
    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="file" id="file"><br>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>
<?php 
$allowedEXTs = array("jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end ($temp);
if ((($_FILES["file"] ["type"] == "image/jpeg")
    || ($_FILES ["file"] ["type"] == "image/jpg")
    || ($_FILES ["file"] ["type"] == "image/png"))
    && ($_FILES ["file"] ["size"] < 10240)
    && in_array($extension, $allowedExts))
{
    if ($_FILES["file"] ["error"] > 0)
    {
        echo "Error: " . $_Files["file"] ["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES ["file"] ["name"] . "<br>";
        echo "Type: " . $_FILES ["file"] ["name"] . "<br>";
        echo "Size: " . $_FILES ["file"] ["size"] / 10240 . " kB<br>";
        echo "Stored in:" . $_FILES ["file"] ["tmp_name"];
    }
    if (file_exists("upload/" . $_FILES["file"]["name"]))
    {
        echo $_FILES ["file"] ["name"] . "already exists.";
    }
    else 
    {
        move_uploaded_files ($_FILES ["file"]["tmp_name"], "upload/" . $_FILES ["file"] ["name"]);
    }
}
else
{
    echo "Invalid file";
}
?>

我基本上不知道为什么这会引发"警告:in_array(( 期望参数 2 是数组,空给出"该错误,任何人都可以伸出援助之手吗?

$allowedEXTs = array("jpeg", "jpg", "png");

这样宣布,

&& in_array($extension, $allowedExts))

这样叫,

您已使用"EXT"声明它并将其称为"Exts">...因此错误。

定义数组

 $allowedEXTs = array("jpeg", "jpg", "png");

然后调用它

&& in_array($extension, $allowedExts))

确保每次调用变量时大小写都相同。


与应$_FILES的这一行相同:

echo "Error: " . $_Files["file"] ["error"] . "<br>";

你用 $allowedEXTs 声明数组,但它在 if 语句中拼写为小。

相关文章: