Check loadable avatars
Just wrote a check for a downloadable avatar. Maybe someone will come in handy. Yes, 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 download 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 format of the file is not JPG and not GIF.";
else
{
/ * get file sizes and check if it is a picture at all * /
@list ($ width, $ height) = getimagesize ($ avatar ['tmp_name']);
if (! is_numeric ($ width) ||! is_numeric ($ height))
$ errors ['avatar'] = “Please upload a picture. Nothing else you just can not download. ;) ";
elseif ($ width! = 100 || $ height! = 100)
$ errors ['avatar'] = "The size of the avatar should be 100 by 100 pixels, we warned. :) ";
}
')
Source: https://habr.com/ru/post/12320/
All Articles