Chetan
Chetan

Reputation: 131

PHP Upload form giving out errors

I,m a total PHP newbie, I learned about arrays yesterday and my boss asked me to write a PHP form, I found one on W3 schools and ended up with

<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";
}
?>

I basically don't know why this is throwing up "Warning: in_array() expects parameter 2 to be array, null given in" that error, can anyone lend a helping hand?

Upvotes: 1

Views: 346

Answers (3)

Aditya Jhunjhunwala
Aditya Jhunjhunwala

Reputation: 635

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

Declared like this,

&& in_array($extension, $allowedExts))

Called like this,

You have declared it with "EXTs" and calling it as "Exts"... Hence the error.

Upvotes: 3

Patrick Geyer
Patrick Geyer

Reputation: 1525

You declare the array with $allowedEXTs, but it is spelled small in the if statement.

Upvotes: 0

Jamie Taylor
Jamie Taylor

Reputation: 4765

You define the array with

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

then call it with

&& in_array($extension, $allowedExts))

Make sure the case is the same every time you call a variable.


Same with this line which should be $_FILES:

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

Upvotes: 1

Related Questions