$_FILES[“file”][“size”] returning 0?

后端 未结 4 1492
借酒劲吻你
借酒劲吻你 2020-12-18 18:29

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

4条回答
  •  天涯浪人
    2020-12-18 19:23

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

提交回复
热议问题