$ _FILES [“文件"] [“大小"]返回0? [英] $_FILES["file"]["size"] returning 0?

查看:261
本文介绍了$ _FILES [“文件"] [“大小"]返回0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP上传某些内容,并设置了允许上传的总大小的限制.我想将上传的文件限制为2MB,但是由于某种原因,每当我尝试使用如下if语句进行检查时:

I am trying to upload something using PHP and set a limit on the total size that I allow to be uploaded. I want to limit my uploads to 2MB but for some reason whenever I try to check with an if statement like this:

if (($_FILES["file"]["size"] < 2097152))

较大的文件(例如7mb文件)将通过if语句,因为无论出于什么原因,如果我打印$_FILES["file"]["size"],它将返回0,而不是正确的字节数.如果我尝试上传较小的内容(例如342kb),则$_FILES["file"]["size"]将返回正确的大小.

A file that is large (such as a 7mb file) will pass through the if statement because for whatever reason if I print $_FILES["file"]["size"], it will return 0, instead of the proper number of bytes. If I try to upload something that is smaller, like 342kb the $_FILES["file"]["size"] will return the proper size.

是否有要获取$_FILES["file"]["size"]的文件的实际大小?否则,我不知道如何解决此问题.

Is there anyway to get $_FILES["file"]["size"] to actually hold the proper size of the file? Otherwise I do not know how to fix this problem.

推荐答案

由于任何原因(上传失败,超出限制等)中止的文件将显示为大小0

A file which aborts for any reason (upload failed, exceeds limits, etc...) will show as size 0

在对其余的上传数据进行任何操作之前,您必须检查上传是否成功:

You have to check for upload SUCCESS before you do ANYTHING with the rest of th eupload data:

if(array_key_exists('file', $_FILES)){
    if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
       echo 'upload was successful';
    } else {
       die("Upload failed with error code " . $_FILES['file']['error']);
    }
}

此处中定义了错误代码.在您的情况下,如果您已硬编码2兆欧的限制并且有人上载了2.1兆的文件,则错误代码将是UPLOAD_ERR_INI_SIZE(又名2),即超出了.ini文件中设置的限制".

The error codes are defined here. In your case, if you've hardcoded a 2meg limit and someone uploads a 2.1 meg file, then the error code would be UPLOAD_ERR_INI_SIZE (aka 2), which is "exceeds limit set in .ini file".

这篇关于$ _FILES [“文件"] [“大小"]返回0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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