Hello, in this video, we are going to take a look at limiting the file size. Because it's natural, if you are accepting uploads from a user, you might want to limit them, you know the file size that they upload, maybe you have them for a tier based system. And on the free account, you can only upload files of five megabytes or less. And on the say, more premium package, you can upload up to, let's say, a gigabyte. And then there is an unlimited file upload size on the Uber duper super premium package. That's one example where you might want to use this.
And also just to restrict, if you know you think your server might get clogged up as well. And you don't want people to be uploading, let's say raw image files, which can be huge when it's just going to be displayed on a phone primarily via an app in a very small and maybe there's gonna be a filter on here as well. Unlike an application like Instagram, then you only need a high file sites. To limit it really simple. All you do is cancel the number check if if dollar underscore files. And first thing you need to do is naturally need to actually get the, you know which file that you are checking.
So you want to do is this one Yep, file uploaded. File uploaded. And now what we are going to do is check if it's bigger than a certain number. So this is measured in bytes. So this is one byte and we can have a look at how big our images so if we go to game for the one that we're going to be uploading is 209 kilobytes. So to do a kilobyte, you would have to do one file And that's one kilobyte.
And that's 10 kilobytes, that's 100 kilobytes. So that will be 200 kilobytes. So let's do 200 kilobytes. So it should not upload, because it's a little too big. And before I go too far, it's going here, put echo on it. file is too large.
Please choose a smaller image. Great tag. And I'm going to say dollar upload status equals zero because it has failed. Therefore, we want to say file was not uploaded. Now, if we go here, choose File one, which is 209 kilobytes. Go to upload says file is too large.
Please do the small image. As you can see, the husband uploaded it, but what I did Notice, which I didn't know is that this other image of God is just a bit under the two kilobyte mark. So without even modifying the code, we're going to try this one and say upload. It doesn't like that one, saying it's too big. So let me make sure I'm going to zeros here. If I wasn't, let me try something else on the work done wrong, because from here, I need to get the size variable here to do so obviously, without getting the sois can actually check it.
So now let's try it again. So if we go back, go to one, upload file too large. As you can see, it hasn't uploaded it. You can see based on the errors that he hasn't, and then number two, which should be So nara sanely successful. There we go. So because that was just 10 kilobytes under the 200 mark, which was the cap that we had set, and that is how you restrict a image file, or any file, really, we are only allowing files.
There are images based on the check that we've done in here. But it can be literally any sort of file could be a text file, can be a video file could be a zip file, anything you want. So that is it. For you have any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next video.