Reputation: 14990
I'm trying to restrict uploaded files with PHP, only to certain types of images. This is what I've tried:
This is the form input field:
<input type="file" id="dni" name="dni">
And this is the checkout process:
if (exif_imagetype($_FILES['dni']["name"]) != IMAGETYPE_GIF || IMAGETYPE_JPEG || IMAGETYPE_PNG) {
echo 'Only images!';
exit;
}
I've tried using $_FILES['dni']["name"]
and $_FILES['dni']["tmp_name"]
And as maybe my condition was not writter in a proper way, I've also tried:
if (exif_imagetype($_FILES['dni']["name"]) != IMAGETYPE_GIF) { ...
And after that I've tried to upload a gif image.
I've tried to upload gif and jpeg images and I keep receiving an error (the "Only images!" message).
I've modified the code, and still won't work:
if (exif_imagetype($_FILES['dni']['name']) != IMAGETYPE_GIF
AND exif_imagetype($_FILES['dni']['name']) != IMAGETYPE_JPEG
AND exif_imagetype($_FILES['dni']['name']) != IMAGETYPE_PNG) {
echo 'Only images!';
exit;
}
Upvotes: 0
Views: 813
Reputation: 771
You're not comparing them right, try this:
if (exif_imagetype($_FILES["dni"]["tmp_name"]) != IMAGETYPE_GIF && exif_imagetype($_FILES["dni"]["tmp_name"]) != IMAGETYPE_JPEG && exif_imagetype($_FILES["dni"]["tmp_name"]) != IMAGETYPE_PNG) {
echo 'Only images!';
exit;
}
You used || (OR) instead of && (AND). It needs to display the error if the imagetype does not equal all those. If you use an OR it will always display it because it can't be all those types at once.
Also, seems $_FILES["dni"]["tmp_name"] is correct. Try changing your ' to a "
Upvotes: 2