PHP Image size is less than 1mb

2019-06-14 20:58发布

问题:

curretly I am using the following to workout if the file size is less than 1MB however as the following code was from 9lession example site it said to check the size for 1mb, but if I times 1024*2 which is what they are doing here it equals not 1mb but 2048kb

saying that the size that it uploads is not in kb instead it from my understanding is bits

if you're confused so am I. I need a simple way to tell if an image is 1mb size

if($size<(1024*1024))

image that i uploaded and its size:

**Size:**10514

Also the reason I don't want to set it on a server level because we also want to do videos. We are still working our the max-size limit we will accept for videos.

回答1:

1MB == 1048576 bytes

1MB == 1024 Kbytes

You question is not clear, but I will just improvise anyhow.

If you want to restrict file uploads to below < 1MB only!! then, since the $_FILES array, will output in bytes you can do the following.

if($_FILES['name']['size'] > 1048576){
  //You can not upload this file
}

Or you want want to restrict it from browser-level, you can add an attribute to your form as

form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
    <input type="file" name="pictures" />
    <input type="submit" value="upload" />
</form>

Offcourse, the second option can be changed by anyone easily, and should never be used.