Skip Navigation

[Resolved] Php function for limiting size of images uploaded NOT working

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the Forms API to validate each image that is uploaded in a single or repeating custom field. It doesn't seem to be working, in the logs I see a PHP notice about trying to get property of a non-object.

Solution: Make sure the User has permission to publish Media if you are using Toolset Access Control. Make sure the Form's markup structure doesn't include any extra closing div tags. Use the following custom code to validate a repeating image field:

add_filter('cred_form_ajax_upload_validate','rep_img_size_validation',10,2);
function rep_img_size_validation($error_fields, $form_data)
{
error_log(' validation script is invoked ');
  $forms = array( 29, 49 );
  $size = 1024000;
  $field_slug = 'your-repeating-image-slug';
  $error_msg = 'Maximum size is 1MB, please try again.';
  
  //field data are field values and errors
  list($fields,$errors)=$error_fields;
  //validate if specific form
  if (in_array( $form_data['id'], $forms ) && isset($fields['wpcf-' . $field_slug]['field_data']['size']))
  {
    //check if this field instance img is bigger than $size (bytes)
    $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
    if ( $instance_size > $size )
    {  
      //display error message for this field instance
      $errors['wpcf-' . $field_slug] = $error_msg;  
    }
  }
  //return result
  return array($fields,$errors);
}

To validate a single field, copy the code above and make the following adjustments:
- Change the function names to prevent a PHP error
- Change the field slug
- Remove array_pop from the instance_size definition like this:

// for repeating images
// $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
// for single images
$instance_size = $fields['wpcf-' . $field_slug]['field_data']['size'];

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_ajax_upload_validate

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1232939

This video explains: hidden link

This is the thread I tried using the function: https://toolset.com/forums/topic/limiting-the-size-of-images-being-uploaded-with-cred-form/

#1233543

Hi, let's try these troubleshooting steps:
- Temporarily disable any custom JavaScript you have included in the Form's JS panel and be sure no JavaScript errors are triggered when the image is uploaded
- Check to be sure the "form_messages" field is included in the Form editor. This field is required for custom validation.
- Turn on PHP logs and add some debug code so we can trace the flow. If you are not familiar with logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for

define('WP_DEBUG', false);

Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Now add error_log statements in your validation code, like this:

add_filter('cred_form_ajax_upload_validate','rep_img_size_validation',10,2);
function rep_img_size_validation($error_fields, $form_data)
{
error_log(' validation script is invoked ');
  $forms = array( 29, 49 );
  $size = 1024000;
  $field_slug = 'your-repeating-image-slug';
  $error_msg = 'Maximum size is 1MB, please try again.';
 
  //field data are field values and errors
  list($fields,$errors)=$error_fields;
  //validate if specific form
  if (in_array( $form_data['id'], $forms ) && isset($fields['wpcf-' . $field_slug]['field_data']['size']))
  {
error_log(' this is one of the forms where the image size validation should be applied ');
    //check if this field instance img is bigger than $size (bytes)
    $instance_size = array_pop($fields['wpcf-' . $field_slug]['field_data']['size']);
error_log(' instance size: ' . $instance_size );
    if ( $instance_size > $size )
    {
 
      //display error message for this field instance
      $errors['wpcf-' . $field_slug] = $error_msg;
 
    }
  }
  //return result
  return array($fields,$errors);
}

Refresh the page and try to upload the image again. This process should automatically create an error_log.txt file in your site's root directory. Please copy and paste its contents into your next reply. Once that is done, you can revert the changes you made to wp-config.php and delete the log file.

#1233799

Followed the instructions to the T... this is what the error log showed:

[22-Apr-2019 12:28:00 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 12:28:00 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 12:28:00 UTC] validation script is invoked
[22-Apr-2019 12:28:34 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 12:28:34 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 12:28:34 UTC] validation script is invoked

#1233925

Okay thanks, a few follow up questions:
- Is this a repeating image field, or is only one image allowed in this field?
- Is this a generic image field, or an existing image custom field?

Look for this line in the code:

list($fields,$errors)=$error_fields;

Just after that line, add this code:

error_log( print_r( $error_fields, true) );

Please upload the image again and let me know what you find in the logs.

#1233979

My form has one non-repeating custom image field and one repeating custom image field. I would like to limit both from accepting over 1mb pictures.

I added the code you provided.

Here is what the logs showed:
[22-Apr-2019 20:26:59 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 20:26:59 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 20:26:59 UTC] validation script is invoked
[22-Apr-2019 20:26:59 UTC] Array
(
[0] => Array
(
[wpcf-foto-perfil] => Array
(
[field_data] => Array
(
[name] => Background_fallback_video.png
[type] => image/png
[tmp_name] => /tmp/phpgbl961
[error] => 0
[size] => 1938331
)

)

)

[1] => Array
(
)

)

[22-Apr-2019 20:27:07 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 127
[22-Apr-2019 20:27:07 UTC] PHP Notice: Trying to get property of non-object in /home/b16f1985/public_html/felipepenna.com/destackterapiastest/wp-content/plugins/cred-frontend-editor/application/controllers/frontend/file_ajax_upload_manager.php on line 135
[22-Apr-2019 20:27:07 UTC] validation script is invoked
[22-Apr-2019 20:27:07 UTC] Array
(
[0] => Array
(
[wpcf-fotos-para-anuncio] => Array
(
[field_data] => Array
(
[name] => Array
(
[0] => Agencia_modelos.jpeg
)

[type] => Array
(
[0] => image/jpeg
)

[tmp_name] => Array
(
[0] => /tmp/phpXlJNvm
)

[error] => Array
(
[0] => 0
)

[size] => Array
(
[0] => 2457250
)

)

)

)

[1] => Array
(
)

)

#1234687

Okay at this point I think it's probably best for me to log in and take a look at how this is set up. Please provide login credentials in the private reply fields here. Let me know where I can find this Form on the front-end of the site.

#1235283

I'm seeing a problem where the API callback isn't receiving the correct Form ID. That's not normal, and I'd like to figure out why that's happening. Is it okay for me to install the Duplicator plugin and create a clone of your site? I can run additional tests locally without affecting your live site.

#1235299

Yes, I already have that plugin. Do what you need to do.

#1235311

Thanks, I'm escalating this to the second tier support team and I'll let you know what I find out.

#1235607

Okay my 2nd tier team was able to pin down the problem with the Form ID. It turns out there is an extra closing div in the Form contents that is causing problems with the parser. Please remove the last closing div tag before the end of the form contents. Then the validation script should begin working as expected for the repeating image field. Please confirm, then we can move on to validating the single image field.

#1235609

I removed the last </div> but it's not working. Actually now I can't even upload any picture at all, not even a 17kb size picture.

#1235612
Screen Shot 2019-04-25 at 9.51.17 AM.png
Screen Shot 2019-04-25 at 9.51.05 AM.png

It's working for me, here are two screenshots. Can you clear your cache and try again?

#1235628
erro_uploading_smaller.jpg

The message is working if the file is bigger than 1MB, that is working but I'm trying to add one smaller than that and it gives an error: "There was an error uploading your file."

I did clear my cache and used incognito browser chrome, but it still says that.

#1235681
300x200.png

- Are you testing while logged in or logged out?
- I am attaching an image here that is less than 1Mb, please try it and let me know the results.
- Can you attach the image you are uploading so I can test using the same image?

#1235698

I show you in this video: hidden link

Are you testing while logged in or logged out?
I have to be logged in as a User or Admin to get to that page.

I am attaching an image here that is less than 1Mb, please try it and let me know the results.
I've shown in the video above using your image that it doesn not work.

Can you attach the image you are uploading so I can test using the same image?
I can't attach the image I tried because it's over 1MB.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.