PHP的图像大小小于1MB [英] PHP Image size is less than 1mb

查看:165
本文介绍了PHP的图像大小小于1MB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文件大小小于1MB,我使用下面的方法来锻炼,但是下面的代码是来自9lession示例站点,它表示检查大小为1mb,但是如果我乘以1024 * 2这就是他们是什么在这里做,它不等于1mb,但2048kb

它说上传的大小不是kb,而是从我理解的是位



如果您感到困惑,那么我也是。我需要一个简单的方法来判断一个图像是否为1mb大小

  if($ size <(1024 * 1024))

我上传的图片及其大小:



**大小:** 10514



另外我不想把它设置在一个服务器级别,因为我们也想做视频。我们仍然在处理我们接受视频的最大尺寸限制。

code> == 1048576 bytes

1MB == 1024 Kbytes



不清楚,但我会随便凑合。

如果您想将文件上传限制在< 1MB 只!那么,由于 $ _ FILES 数组将以字节输出,因此您可以执行以下操作。

  if($ _ FILES ['name'] ['size']> 1048576){
// You不能上传这个文件



$ b

或者你想限制它从浏览器级别,你可以添加一个属性到你的表单中:

  form method =POSTenctype =multipart / form-data> ; 
< input type =hiddenname =MAX_FILE_SIZEvalue =1048576/>
< input type =filename =pictures/>
< input type =submitvalue =upload/>
< / form>

Offcourse,第二个选项可以被任何人轻易改变,不应该被使用。

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.

解决方案

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.

这篇关于PHP的图像大小小于1MB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆