Is there a way to simplify this condition?

I am making an image upload script, and was thinking there was a way to turn this into an array. Is that possible, or was I wrong?

if ($filetype != "image/gif" || $filetype != "image/jpg" || $filetype != "image/png" || $filetype != "image/jpeg") {

Not tested but something like@


$img_filetypes = array(
          "image/gif"
        , "image/jpg"
        , "image/png"
        , "image/jpeg"
    );
    
if(!in_array($filetype, $img_filetypes)) {
    
    //
    
}

Yup you sure could do that


$types=array("image/gif", "image/jpg", "image/png", "image/jpeg");
if (in_array($type, $types)) { 
   // do something fun
} else {
   // thou shalt not upload non-image files!
}

:cool:

Edit:

spikeZ beat me to it (:

Awesome. I’m fairly new to arrays, and wasn’t unsure if this would work in it.

Simplified my code a little bit, thanks!

wo0ot! :slight_smile:

By the way, in your opinion, how big of a file should I allow to be uploaded?

I’m thinking somewhere around 250-500kb because my server will have unlimited storage and bandwidth. My site is based around people uploading and sharing images. Unless there is a way I can have my code resize them a little smaller automatically?

Yes on most servers you can [make images smaller], using the GD library.
Take a look here: PHP: GD - Manual

Normally restrictions are defined in php settings files of file upload and post (2mb and 8mb)
You should not forget about users internet connection as well as you site may be used in remote africa as well…
sometimes people try to upload 1mb, 3000x2000 photo directly from digital camera memory so make sure that you check the max dimension as well…
and when you upload think of least internet connection your user might have ,not your internet connection.
If you try uploading 1mb files through 64kbps internet i wonder it will get uploaded at one go(or will need atleast animated progress bar to provide some clue to user)

thanks