Limit File Type

3 minutes
Share the link to this page
Copied
  Completed
Now we will restrict the type of images acceptable

Transcript

Hello, in this video we are going to look at limiting the file type. So we already have essentially a check here, which checks if it's an image or not. But as you know, there are different image types. You can have a PNG or jpg, you can have a GIF and TIFF. And you know, there are other formats as well, if you only want a specific format, then this won't handle this, this will only check is basically an image now, but we're going to implement if it is a particular format, and that's simple, you just check the extension for you this image file type. So we can just do it here really, all going to say is F image file type isn't equal to and then we actually put the actual file type.

So let's put JPG And so it's not equal to any of the valid formats, then we'll provide an error. And we also want to do JPEG two same thing, that jpg, basically, you know, it can be a bit confusing because you could have technically both formats. Going to the bottom don't want that. And here, we could also do a PNG. So these are the formats that we are going to accept. So if it isn't one of these, so basically, if you files, you know, these checks, then we'll go into here, and we'll say echo, not a valid image type must be jpg, JPEG, or PNG, please, please, again and we are going to say The upload status is equal to zero.

So what I have is copy images on the desktop one PNG one, the GIF, the PNG should upload successfully, the other one well shouldn't. So if I go to upload, go to desktop, go to PNG, go to upload image, and if it said it's been successful, there we go. So I'm going to go back to the homepage, again, trying to upload the GIF, which is a image, but it's not part of our valid formats, valid extensions. So if I select Jeff, Scott got it upload image, not a valid image type is given us the error. And I do yet the era we want to put a grapevine but that's just formatting that don't really matter. As you can see, it has not uploaded the image because it was not a valid type here.

So that is it. That's all you need to do. You can do this for another images, videos, text files, anything that you want. What I want you to do as an extra task is essentially abstract out what we've got here. I know the valid formats jpg, jpg, and PNG into an array. And then we're going to loop over the array.

So you can easily just add a valid or invalid extension, you know, into the array instead of having to do these extra checks. You'll probably need to use some sort of loop for that. So how about that you have any questions, feel free to drop me a message. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.