3

How can I catch an error on a file uploaded to my web server that is greater than php upload_max_filesize?

My question is similar to so/large-file-upload-errors-with-php although my memory limit is set to 512M so the resolution that question used will not help me.

I am trying to upload a file 6.9MB for example and my upload_max_filesize = 6M. My script basically just stops executing and I cannot tell where or why. Also, I have error reporting turned on.

Also I should note that files <6MB I can upload and process correctly with the following code:

if(isset($_FILES['file']['name']) and !empty($_FILES['file']['name'])){

    $info = pathinfo($_FILES['file']['name']); 
    $ext = $info['extension'];
    //verify file is of allowed types 
    if(Mimetypes::isAllowed($ext)){
        if(filesize($_FILES['file']['tmp_name']) <= AttachmentUploader::$maxFilesize){              
            $a = new AttachmentUploader();      //for file uploading 
            if($a->uploadFile($_FILES['file'], 'incident', $_POST['sys_id'])){
                header("location: ".$links['status']."?item=incident&action=update&status=1&place=".urlencode($links['record']."id=".$_POST['sys_id']));            
            }else{
                header("location: ".$links['status']."?item=incident&action=update&status=-1&place=".urlencode($links['record']."id=".$_POST['sys_id']));           
            }

        }else{      
            $errors[] = 'The file you attempted to upload is too large.  0.5MB is the maximum allowed size for a file.  If you are trying to upload an image, it may need to be scaled down.';
        }
    }else{
        $errors[] = 'The file you attempted to upload is not allowed.  Acceptable extensions: jpg, gif, bmp, png, xls, doc, docx, txt, pdf';
    }
}else{
    $errors[] = 'Please attach a file.';
}

My php.ini settings:

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 7200     ; Maximum execution time of each script, in seconds
memory_limit = 512M  ; Maximum amount of memory a script may consume (8MB)


;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = /tmp

; Maximum allowed size for uploaded files.
upload_max_filesize = 6M
2
  • Does your webserver log any errors? Are you over max_input_time? Commented Sep 21, 2010 at 16:55
  • I cannot fathom I am over max input time as I am on a gigabit lan to the server and a file > 6MB is going almost instantly. I will check into it though. Commented Sep 21, 2010 at 16:59

1 Answer 1

5

The error is in $_FILES['userfile']['error']. You just have to check that this value is UPLOAD_ERR_INI_SIZE to detect if the file is bigger than the max size defined in your php.ini.


Resources :

Sign up to request clarification or add additional context in comments.

4 Comments

Is it always 'userfile' or would it be the name of the input field ?
It's based on the field name. For you it will be $_FILES['file']['error']
After checking the submit was pressed I have if($_FILES['file']['error'] === UPLOAD_ERR_OK){echo "error";}else{echo "no error";} my script is not echoing either value.
Check that the post_max_size is larger than upload_max_filesize. If these values are the same and the file exceeds the limit, it will cause the $_FILES array (and $_POST) to be empty. php.net/manual/en/ini.core.php#ini.post-max-size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.