PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Catch upload file size exceeded error

  1. Hi,
    I realise this has been discussed extensively, and I've read all forum posts I could find regarding this - however ...

    I understand the issue of PHP uploader not knowing filesize until it's uploaded, and therefore GF not having any "built-in" way of restricting upload file size. (I look forward to the mentioned Flash-based solution in the future...)

    So, the only remaining option is to set the PHP limit in my htaccess file:

    php_value upload_max_filesize 50K

    This works of course, and if I upload a file over 50K the file does not get written to the GF upload directory.

    However, the form submits fine and the user is completely unaware that their file upload has in fact failed.

    Is there anyway to catch this error (server-side, obviously) and display a validation error? Using one of the GF hooks?

    If not, then I would consider this a major flaw in the current version of GF. Essentially being no way to limit upload file size (and gracefully catch the error, inform user, etc)

    Is there a solution?

    Thanks in advance.

    Posted 9 years ago on Saturday April 2, 2011 | Permalink
  2. For the record, when viewing the entry for a submission where file upload size has been exceeded, the GF admin displays:

    FAILED (Temporary file could not be copied.)

    This is not a permissions or safe-mode related error, because if I upload a file within the file upload size limit, it works fine.

    I really hope there's a way to catch this error, or my project just hit a major problem :-(

    Posted 9 years ago on Saturday April 2, 2011 | Permalink
  3. You could write your own custom validation to check for the value and check if it is "FAILED..." and then throw an error if it is. The gform_validation hook is documented here:

    http://www.gravityhelp.com/documentation/page/Gform_validation

    We can certainly look to enhance the error handling for this in a future release. However, the reason we have not is because we wouldn't know why it failed because what you see is the error the server gives us. So we wouldn't know if it was file size related, or permissions related, or what. So we wouldn't exactly know what friendly message to display to the user.

    We are planning on enhancing the file upload field to support multiple files, size limitations, etc. later this year.

    Posted 9 years ago on Monday April 4, 2011 | Permalink
  4. Thanks Carl I'll try my own validation. If I do this, does it replace ALL the possible validation message, such as if field is empty, wrong file extension etc? So, I need to rewrite it completely?

    Posted 9 years ago on Monday April 4, 2011 | Permalink
  5. If you use the gform_validation hook this occurs after any default validation associated with the field.

    Posted 9 years ago on Monday April 4, 2011 | Permalink
  6. Hi,
    I'm trying that sample code from the docs:

    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        // set the form validation to false
        $validation_result["is_valid"] = false;
        $form = $validation_result["form"];
    
        // specify the first field to be invalid and provide custom validation message
        $form["fields"][0]["failed_validation"] = true;
        $form["fields"][0]["validation_message"] = "TEST This field is invalid!";
    
        // update the form in the validation result with the form object you modified
        $validation_result["form"] = $form;
    
        return $validation_result;
    
    }

    it just doesn't seem to get called at all?

    Can you help?

    I am successfully using this in my functions.php, so I pretty sure I'm doing it right:

    function change_message_2($message, $form){
      return "<div class=\"woo-sc-box note\">Please check the form & try again. Errors are noted below.</div>";
    }
    add_filter("gform_validation_message_2", "change_message_2", 10, 2);
    Posted 9 years ago on Tuesday April 5, 2011 | Permalink
  7. can you provide more details instructions on how I would catch the validation message for a given field, check if it's value is "FAILED..." and then re-write the validation message accordingly?

    Posted 9 years ago on Tuesday April 5, 2011 | Permalink
  8. Hi, just to update, I think I've got the php sorted to write the validation, but for some reason this filter just isn't working at all now... I need to trouble shoot, disable plugins etc. thanks

    Posted 9 years ago on Tuesday April 5, 2011 | Permalink
  9. Let us know what you find, if you need assistance post here and we can take a look.

    Posted 9 years ago on Tuesday April 5, 2011 | Permalink
  10. sascha
    Member

    Hi there,
    I am running into the same issue. When a user submits a file that exceeds the allowed size set on the server, there is no error message displayed. There is an error message for the wrong file extension though. Could the size of the file be checked too? Has anyone got this working?

    Other note: Could the wrong extension error be shown as soon as the user has chosen a file - rather than once the submit button has been pressed? Is there a hook to achieve this?

    Posted 8 years ago on Wednesday October 26, 2011 | Permalink
  11. Regarding the file type or file extension, you could do some rudimentary checking before submission by adding some jQuery to your form. Gravity Forms would not handle it because it has not been submitted to Gravity Forms yet.

    There has not been functionality added to check the file size of the upload and the advice from Carl remains the best work around I know of right now.

    Posted 8 years ago on Thursday October 27, 2011 | Permalink
  12. Enhancing the file upload field is slated for inclusion in 1.7.

    http://www.gravityhelp.com/forums/topic/file-uploads-max-file-size-restrict-type#post-38992

    WordPress 3.3 has revamped the media handling, and Gravity Forms will likely be able to leverage that.

    Posted 8 years ago on Thursday October 27, 2011 | Permalink
  13. sascha
    Member

    Hi Chris, Carl or anyone really,
    how would I check if an upload has failed using that filter? I understand the filter, but I do not know how to check for a server error.
    Basically how do I check for the upload error that the server throws back? I just would like to prevent the form being submitted if there is an upload error and then just give a message "Your file size might be too big - individual files need to be less than 2MB".
    Any ideas are welcome...

    Posted 8 years ago on Thursday October 27, 2011 | Permalink
  14. sascha
    Member

    Could this be leveraged by GF?

    UPLOAD_ERR_FORM_SIZE Error Code (Use with the MAX_FILE_SIZE Form Field)
    
    The UPLOAD_ERR_FORM_SIZE error code never appears if there is no MAX_FILE_SIZE hidden field in the HTML/XHTML form that contains the <input type="file"> element.
    
    The MAX_FILE_SIZE hidden field is used like this:
    
    <form action="file_upload.php" method="post" enctype="multipart/form-data">
      <p>
        <input name="MAX_FILE_SIZE" value="1048576" type="hidden"/>
        <input name="myFile" type="file"/>
        <input type="submit"/>
      </p>
    </form>

    Together with this explanation: http://www.php.net/manual/en/features.file-upload.errors.php
    But I guess all those error messages happen after the form is actually submitted right?
    There is a thought:
    I put a function in that Gform validation filter, that uploads the file(s) temporarily to the server, then check if any uploads failed (I could check each file I assume - using that MAX_FILE_SIZE Form Field from above) and then set validation to true or false and give an according error message?
    Does that make sense? Is there a Google translate for PHP - like say what you want and it writes the function for you :-)

    Posted 8 years ago on Thursday October 27, 2011 | Permalink
  15. @sascha, I wish there were a Google Translate for PHP like that. Someday maybe.

    Regarding the rest, give it a shot and let us know where you get stuck.

    Posted 8 years ago on Friday October 28, 2011 | Permalink
  16. Can the admins please provide an answer as to how to catch this error... 1.7 is nice and all when it gets here for the time being we need a solution ASAP... please and thank you

    Posted 8 years ago on Wednesday December 14, 2011 | Permalink
  17. Hey gbotica, did figure out how to catch the error?

    Posted 8 years ago on Wednesday December 14, 2011 | Permalink
  18. gbotica - would you be wiling to share your solution to this, if you have one?
    Thanks!

    Posted 8 years ago on Sunday January 15, 2012 | Permalink
  19. allastair
    Member

    I have the same issue regarding file sizes and could use some guidance on how to do the validation as well. The form validation hook seems pretty straightforward but what is the proper syntax to check for the size of the file in an upload field? Has anyone worked this out?

    Posted 8 years ago on Friday May 11, 2012 | Permalink
  20. David Peralty

    allastair - You'll want to look at the PHP tutorials out there for detecting the file size of an uploaded file and comparing it against your own set value. This page might help - http://php.net/manual/en/features.file-upload.php

    Posted 8 years ago on Friday May 11, 2012 | Permalink