I would like to do below validations for single file uploaded using jQuery Uploadify plugin.
- Check for the file type .jpg, .png & .gif
- Check for the file size maximum 2MB only
I have done this through fileExt
& sizeLimit
options, but as the plugin developer says, one can easily bypass the fileExt
validation & server side validation is recommended.
I want to do this using PHP server side scripting language.
I have already checked the documentation of the plugin & nothing helpful found there. Can someone please suggest how to do this ?
Thanks in advance.
I would recommend using the filesize and exif_imagetype functions to assess the file. The following should point you in the right direction:
$up_file = "file_the_user_uploaded";
$two_mb = 2097152;
if (filesize($up_file) < $two_mb) {
if (exif_imagetype($up_file) > 0 && exif_imagetype($up_file) < 4) {
// the file is .gif, .jpg, or .png and less than 2MB
// do something to approve the file upload
}
}
Note that the filesize()
function in PHP returns the size in bytes, thus, the file should be less than $two_mb
bytes. Also, the exif_imagetype()
returns an integer code mapping to the MIME type of the uploaded image: 1, 2, 3, being .gif
, .jpg
, and .png
respectively.