File upload size tweaks for Gravity Forms

One thing about Gravity Forms that really bugged me and a couple of my clients, is the lack of proper file upload error handling. If a file upload fails, for whatever reason, the form just continues it’s routine, leaving the file upload behind. This is not what you want if you rely on your upload for post thumbnails for example.

It would be even better if Gravity Forms added the option to limit the file size as a setting. Reading through the support forums learned me that they are working on it, so hopefully this is just a temporary solution.

This pretty straight forward code extends the validation of a form with upload fields in it.

[sourcecode language=”php”]function custom_validation( $validation_result )
// Get the form out of the validation results
$form = $validation_result[ ‘form’ ];
// Loop trough all the uploaded files
foreach( $_FILES as $key => $file )
if( $file[ ‘error’ ] == 1 )
{ // Error code 1 is a failed upload
// Set validation to false, making the form show the warning
$validation_result[ ‘is_valid’ ] = false;
// Get the id of the upload field that failed
$exploded = explode( ‘_’, $key );
$input_id = $exploded[ 1 ];
// Set the appropriate vars inside our form
$form[ ‘fields’ ][ $input_id ][ ‘failed_validation’ ] = true;
$form[ ‘fields’ ][ $input_id ][ ‘validation_message’ ] = ‘Upload failed. File size?’;
// Put the form back in the validation result and return it
$validation_result[ ‘form’ ] = $form;
return $validation_result;
add_action( ‘gform_validation’, ‘custom_validation’ );[/sourcecode]

Warning: This code is not completely tested and should therefor be considered experimental. Do not use it in a live environment, without proper testing.

How does it check for file upload size?

It comes down to a very simple check, using the $_FILES upload array for errors. Error code 1 is a failed upload, code 0 is a¬†successful¬†upload. I don’t really know what error code 3 is, but it seems that upon no file upload, error code 4 is set. Gravity Forms does it’s job well in handling required fields, so we’re only checking error code 1 in this code.

The way I get the id of the upload field is pretty dirty, but there is no other way I could think of right now. The $_FILES array only carries the name of the upload field, perhaps some other hack could add the id to that?

Any known downsides to this code?

If a field upload field fails, the file name is still stored inside the upload field. The user has to press a ‘delete’ link right next to it to clear it and be able to upload a new file. This is not really a problem, but it would be wonderful to have a way to clear that upload field too, next to showing the error message.

2 replies on “File upload size tweaks for Gravity Forms”

  1. It’s a shame this issue is still not fixed. Have you come up with any other solutions since this post? I would really like some functionality that allows you to restrict file size and/or image size.

  2. Do you have any updates on this?

    I have the same issue, can’t seem to find a solution to restrict image upload size.
    Even the image limit plugins don’t seem to work as GravityForms seems to bypass the usual route to the media library (if using the image upload as featured image).

Comments are closed.