Checking Downloadable Avatars

    Just wrote a check for a downloadable avatar. Maybe someone will come in handy. And I want to hear criticism;)

    $ avatar = (isset ($ _ FILES ['avatar']) && is_array ($ _ FILES ['avatar']))? $ _FILES ['avatar']: null;
    $ errors = array ();
    if ($ avatar ['error'] == 3)
        $ errors ['avatar'] = “The file did not upload to the server. Try again.";
    elseif ($ avatar ['error'] == 2)
        $ errors ['avatar'] = "File size exceeds the allowed 50 Kb.";
    elseif ($ avatar ['error']! = 0)
        $ errors ['avatar'] = "Some unknown error occurred while loading the avatar.";
    elseif (! preg_match ("~ (. *?) \. (jpg | gif) $ ~ i", $ avatar ['name']))
        $ errors ['avatar' ] = "The file format is not JPG or GIF.";
    else
    {
        / * get the file size and check if it’s a picture at all * /
        @list ($ width, $ height) = getimagesize ($ avatar ['tmp_name']);
        if (! is_numeric ($ width) ||! is_numeric ($ height))
            $ errors ['avatar'] = "Please upload a picture. You just can’t download anything else. ;) ";
        elseif ($ width! = 100 || $ height! = 100)
            $ errors ['avatar'] = “The dimensions of the avatar should be 100 by 100 pixels, we warned you. :) ";
    }

    Also popular now: