1

So I posted an earlier question here: upload

And I tried the solutions but now no matter if it's the correct filetype or not, it just says "invalid file". Here's my html:

<form action="upload_file.php" method="post" enctype="multipart/form-data" target="my_iframe">
    Select a file: <input type="file" name="upload">
    <input type="submit">
</form>

And here's my upload_file.php:

$allowedExts = array("doc", "docx");
    $extension = end(explode(".", $_FILES["file"]["upload"]));

    if (($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["file"]["upload"] . "<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("Proposals/" . $_FILES["file"]["upload"]))
            {
                echo $_FILES["file"]["upload"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "Proposals/" . $_FILES["file"]["upload"]);
                echo "Stored in: " . "Proposals/" . $_FILES["file"]["upload"];
            }
        }
    } else {
        echo "Invalid file";
    }

I've also tried:

$extension = pathinfo( $_FILES["file"]["upload"], PATHINFO_EXTENSION);

And did a

die($extension);

On both of them but nothing gets printed. So I have two questions
1) What is wrong with my above code
2) How do I hide the iframe until I receive a response? and then have like a okay button on it to make it go away once the user has read the response? Here's the iframe code if it helps:

<iframe name="my_iframe" src="upload_file.php"></iframe>

4 Answers 4

2

Change

$_FILES["file"]["upload"]

To

$_FILES["upload"]["name"]

Everywhere, and same with the other references.. So;

$_FILES["file"]["size"] **To** $_FILES["upload"]["size"]

And

$_FILES["file"]["type"] **To** $_FILES["upload"]["type"]

And

$_FILES["file"]["tmp_name"] **To** $_FILES["upload"]["tmp_name"]
Sign up to request clarification or add additional context in comments.

1 Comment

Search and replace $_FILES["file"] with $_FILES['upload'] should do the trick
1

You have:

if (($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts)) {

It should be $_FILES["upload"]["size"] because you have

<input type="file" name="upload">

Comments

1

Check this tutorial : http://www.w3schools.com/php/php_file_upload.asp

It should be $_FILES["upload"] A mistake you have done

Comments

1

Change

$_FILES["file"]["size"]

to

$_FILES["upload"]["size"]

and do the same for all other vars, the name attribute of your input file is upload, not file, according to your HTML:

<input type="file" name="upload">
                  HERE ----^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.