Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

php - $_FILES["file"]["size"] returning 0?

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))

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']);
    }
}

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...