Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

swf file upload

Hello,

I want to upload a .swf file:

        $allowedExt = "swf";
        $temp = explode(".", $_FILES["model"]["name"]);
        $extension = end($temp);

        $modelName = $_FILES["model"]["name"];
        $modelType = $_FILES["model"]["type"];

        //Test echo
        echo $modelType;
        echo $_FILES["model"]["type"];

        //Checks if the file is valid
        if (($modelType == "application/x-shockwave-flash")
        && ($_FILES["model"]["size"] < 200000)
        && in_array($extension, $allowedExt)) {

            if ($_FILES["model"]["error"] > 0) {
                echo "Error: " . $_FILES["image"]["error"] . "<br>";
            } else {
                $modelName = $_FILES["model"]["name"];

                if (file_exists("models/" . $modelName)) {
                    echo $modelName . " already exists. ";
                } else {
                    move_uploaded_file($_FILES["model"]["tmp_name"],
                        "models/" . $modelName);
                    echo "Stored in: " . "models/" . $modelName;
                }
            }
        }else{
            echo "Invalid file";
        }

I don't see anything wrong but i always get "Invalid file".

2 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

you are checking if the extension is in the array, however you are checking against a string and not an array.

you have..

$allowedExt = "swf";  // this creates $allowedExt as a string

Then you check your array by using...

in_array($extension, $allowedExt)  // however in_array() needs a string and and then an array to check against.

Since in_array() needs a string and then an array, and you provided a string and a string , it's going to error out.

What you need to do is ...

$allowedExt = array("swf");  //now $allowedExt is an array

With $allowedExt set as an array, you can use it as your haystack in the in_array() function.

hope that helps.

Hello,

Thanks for the answer. I saw that a litle after I posted the question. The main problem however was the php.ini settings.The maximum size of upload was 2MB and my swf files are bigger so i just had to change that.