PHP 图像大小小于 1mb

2023-12-01php开发问题
0

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

问题描述

目前,如果文件大小小于 1MB,我正在使用以下代码进行锻炼,但是因为以下代码来自 9lession 示例站点,它说要检查 1mb 的大小,但是如果我乘以 1024*2,它们就是在这里做的不是1mb而是2048kb

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

说它上传的大小不是kb,而是我理解的比特

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

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

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:

**尺寸:**10514

**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 千字节

你的问题不清楚,但无论如何我都会即兴发挥.

1MB == 1048576 bytes

1MB == 1024 Kbytes

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

如果你想限制文件上传到 <仅 1MB!!然后,由于 $_FILES 数组将以字节为单位输出,您可以执行以下操作.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17