Accueil › Toolset Professional Support › [Résolu] Image Upload Size Validation Not Working
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.
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)
Marqué : Content-submission forms, Toolset Forms
Documentation connexe :
Ce sujet contient 11 réponses, a 3 voix.
Dernière mise à jour par julieP Il y a 6 années et 2 mois.
Assisté par: Christian Cox.
I want to stop users uploading large images at the point where they try to upload on a Form (not when they submit the form - by then the image has already been uploaded). This is my code but it's not working:-
add_filter('cred_form_ajax_upload_validate','file_type_size_validation_521_529_528',10,2); function file_type_size_validation_521_529_528($field_data, $form_data) { // Field data are field values and errors list($fields,$errors)=$field_data; if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-logo']['type'])) && (isset($_FILES['wpcf-logo']['size']))) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-logo']['type']; //Retrieve file size $file_size_uploaded=$_FILES['wpcf-logo']['size']; //Validate files uploaded, make sure its jpg file type AND not more than 20KB if (!( ('image/jpg' == $file_type_uploaded) && ($file_size_uploaded < 20480) )) { //remove wpcf - prefix here! $errors['logo']='Your image must be a JPG and not exceed 20KB in size'; } } if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-image-1']['type'])) && (isset($_FILES['wpcf-image-1']['size']))) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-image-1']['type']; //Retrieve file size $file_size_uploaded=$_FILES['wpcf-image-1']['size']; //Validate files uploaded, make sure its jpg file type AND not more than 20KB if (!( ('image/jpg' == $file_type_uploaded) && ($file_size_uploaded < 20480) )) { //remove wpcf - prefix here! $errors['image-1']='Your image must be a JPG and not exceed 20KB in size'; } } if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-image-2']['type'])) && (isset($_FILES['wpcf-image-2']['size']))) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-image-2']['type']; //Retrieve file size $file_size_uploaded=$_FILES['wpcf-image-2']['size']; //Validate files uploaded, make sure its jpg file type AND not more than 20KB if (!( ('image/jpg' == $file_type_uploaded) && ($file_size_uploaded < 20480) )) { //remove wpcf - prefix here! $errors['image-2']='Your image must be a JPG and not exceed 20KB in size'; } } if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-image-3']['type'])) && (isset($_FILES['wpcf-image-3']['size']))) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-image-3']['type']; //Retrieve file size $file_size_uploaded=$_FILES['wpcf-image-3']['size']; //Validate files uploaded, make sure its jpg file type AND not more than 20KB if (!( ('image/jpg' == $file_type_uploaded) && ($file_size_uploaded < 20480) )) { //remove wpcf - prefix here! $errors['image-3']='Your image must be a JPG and not exceed 20KB in size'; } } //return result return array($fields,$errors); }
Any ideas please? Thanks
Try the mime type 'image/jpeg' with the 'e' instead of 'image/jpg'. That's the first thing I noticed. If it doesn't solve the problem we can add some error logging to the code to see where things are breaking down.
Hi Christian
I'm afraid that made no difference. How do I add error logging to the code please?
Hi Julie,
Christian is currently on a public holiday today but he will be back tomorrow to continue helping here.
Thanks,
Shane
Here is an example showing some error log statements added to the first conditional:
add_filter('cred_form_ajax_upload_validate','file_type_size_validation_521_529_528',10,2); function file_type_size_validation_521_529_528($field_data, $form_data) { error_log('ajax validation triggered'); error_log(print_r($_FILES, true)); // Field data are field values and errors list($fields,$errors)=$field_data; if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-logo']['type'])) && (isset($_FILES['wpcf-logo']['size']))) { error_log('in array 1'); //Retrieve file type $file_type_uploaded=$_FILES['wpcf-logo']['type']; error_log('file type uploaded: ' . $file_type_uploaded); //Retrieve file size $file_size_uploaded=$_FILES['wpcf-logo']['size']; error_log('file size: ' . $file_size_uploaded); //Validate files uploaded, make sure its jpg file type AND not more than 20KB if (!( ('image/jpeg' == $file_type_uploaded) && ($file_size_uploaded < 20480) )) { //remove wpcf - prefix here! $errors['logo']='Your image must be a JPG and not exceed 20KB in size'; } }
Add similar logs to each conditional so we can observe the code execution in the logs. If you're not sure how to turn on error logging, 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');
Upload an image using the Form and a file called "error_log.txt" will be created in your site's root directory. Send me its contents.
Hi Christian
Thank you for this.
After adding the lines to the conditional, I tried uploading a small PNG, then a large PNG for the first image, a large JPG for each of the 3 other images. None of these meet the required criteria.
The error_log.txt wasn't created but here's the contents of my debug.log file:-
[05-Sep-2018 03:29:26 UTC] ajax validation triggered [05-Sep-2018 03:29:26 UTC] Array ( [wpcf-logo] => Array ( [name] => back-to-top.png [type] => image/png [tmp_name] => /tmp/phpHiWxTn [error] => 0 [size] => 971 ) ) [05-Sep-2018 03:29:26 UTC] in array 1 [05-Sep-2018 03:29:26 UTC] file type uploaded: image/png [05-Sep-2018 03:29:26 UTC] file size: 971 [05-Sep-2018 03:32:09 UTC] ajax validation triggered [05-Sep-2018 03:32:09 UTC] Array ( [wpcf-logo] => Array ( [name] => Dashboard #1.png [type] => image/png [tmp_name] => /tmp/phpjaT79I [error] => 0 [size] => 213895 ) ) [05-Sep-2018 03:32:09 UTC] in array 1 [05-Sep-2018 03:32:09 UTC] file type uploaded: image/png [05-Sep-2018 03:32:09 UTC] file size: 213895 [05-Sep-2018 03:33:58 UTC] ajax validation triggered [05-Sep-2018 03:33:58 UTC] Array ( [wpcf-logo] => Array ( [name] => Dashboard #1.jpg [type] => image/jpeg [tmp_name] => /tmp/phpQPCm7A [error] => 0 [size] => 43285 ) ) [05-Sep-2018 03:33:58 UTC] in array 1 [05-Sep-2018 03:33:58 UTC] file type uploaded: image/jpeg [05-Sep-2018 03:33:58 UTC] file size: 43285 [05-Sep-2018 03:37:51 UTC] ajax validation triggered [05-Sep-2018 03:37:51 UTC] Array ( [wpcf-image-1] => Array ( [name] => Dashboard #1.jpg [type] => image/jpeg [tmp_name] => /tmp/php0fnUYo [error] => 0 [size] => 43285 ) ) [05-Sep-2018 03:37:51 UTC] in array 2 [05-Sep-2018 03:37:51 UTC] file type uploaded: image/jpeg [05-Sep-2018 03:37:51 UTC] file size: 43285 [05-Sep-2018 03:38:00 UTC] ajax validation triggered [05-Sep-2018 03:38:00 UTC] Array ( [wpcf-image-2] => Array ( [name] => Dashboard #2.jpg [type] => image/jpeg [tmp_name] => /tmp/phpL5N0DI [error] => 0 [size] => 59637 ) ) [05-Sep-2018 03:38:00 UTC] in array 3 [05-Sep-2018 03:38:00 UTC] file type uploaded: image/jpeg [05-Sep-2018 03:38:00 UTC] file size: 59637 [05-Sep-2018 03:38:09 UTC] ajax validation triggered [05-Sep-2018 03:38:09 UTC] Array ( [wpcf-image-3] => Array ( [name] => customer-profiles.jpg [type] => image/jpeg [tmp_name] => /tmp/phprwTuTo [error] => 0 [size] => 56295 ) ) [05-Sep-2018 03:38:09 UTC] in array 4 [05-Sep-2018 03:38:09 UTC] file type uploaded: image/jpeg [05-Sep-2018 03:38:09 UTC] file size: 56295
Okay the debug information looks good. Let's simplify the conditional and add another log inside the condition:
if ( ('image/jpeg' != $file_type_uploaded) || ($file_size_uploaded > 20480) ) { error_log('error conditions met'); //remove wpcf - prefix here! $errors['logo']='Your image must be a JPG and not exceed 20KB in size'; }
Hi Christian
I'm not sure I've understood how to implement your suggestion correctly whilst applying it to the required form IDs. Is this what you meant? (I've reduced the hook for the purposes of this thread by applying it to just one of the images on the form)
add_filter('cred_form_ajax_upload_validate','file_type_size_validation_521_529_528',10,2); function file_type_size_validation_521_529_528($field_data, $form_data) { // Field data are field values and errors list($fields,$errors)=$field_data; if (in_array($form_data['id'], [521,529,528]) && ('image/jpeg' != $file_type_uploaded) || ($file_size_uploaded > 20480)) { error_log('error conditions met'); //remove wpcf - prefix here! $errors['logo']='Your image must be a JPG and not exceed 20KB in size'; } //return result return array($fields,$errors); }
Try this and let me know the resulting logs when you upload the logo field image:
add_filter('cred_form_ajax_upload_validate','file_type_size_validation_521_529_528',10,2); function file_type_size_validation_521_529_528($field_data, $form_data) { // Field data are field values and errors list($fields,$errors)=$field_data; if (in_array($form_data['id'], array(521,529,528)){ if(isset($_FILES['wpcf-logo']['type']) && isset($_FILES['wpcf-logo']['size'])) { //Retrieve file type $file_type_uploaded=$_FILES['wpcf-logo']['type']; error_log('file type uploaded: ' . $file_type_uploaded); //Retrieve file size $file_size_uploaded=$_FILES['wpcf-logo']['size']; error_log('file size: ' . $file_size_uploaded); //Validate files uploaded, make sure its jpg file type AND not more than 20KB if ( ( $file_type_uploaded != 'image/jpeg' ) || ( $file_size_uploaded > 20480 ) ) { error_log('error conditions met'); //remove wpcf - prefix here! $errors['logo']='Your image must be a JPG and not exceed 20KB in size'; } } } //return result return array($fields,$errors); }
Hi Christian
Sorry; I misread your instruction.
I had to change this
if (in_array($form_data['id'], array(521,529,528)) { if(isset($_FILES['wpcf-logo']['type']) && isset($_FILES['wpcf-logo']['size'])) {
back to this
if (in_array($form_data['id'], [521,529,528]) && (isset($_FILES['wpcf-logo']['type'])) && (isset($_FILES['wpcf-logo']['size']))) {
because I was getting a warning about an unexpected T_IF in the editor.
Tried again and images were uploaded to media library. Here's the error log content:-
[07-Sep-2018 03:47:59 UTC] file type uploaded: image/png [07-Sep-2018 03:47:59 UTC] file size: 213895 [07-Sep-2018 03:47:59 UTC] error conditions met [07-Sep-2018 03:48:13 UTC] file type uploaded: image/jpeg [07-Sep-2018 03:48:13 UTC] file size: 43285 [07-Sep-2018 03:48:13 UTC] error conditions met
Any ideas?
PS: I notice you changed the final arguments & I think the OR condition is better so thank you for that.
Okay so the error conditions are met, but the Form displays no errors and the images are uploaded to the media library successfully, correct? I can't replicate that on my test multisite environment so I suspect something else is going on. Please make sure you have updated to the latest versions of all Toolset plugins, deactivate all non-Toolset plugins temporarily and try again. Watch the browser console to see if any JavaScript errors are displayed. Open the network tab and watch the admin-ajax.php request when the image is uploaded. Copy + paste the response here for me to review.
Hi Christian
After more testing, I managed to track the issue back to another hook being applied to image uploads on a different form. The format of that hook was different to the one here. Once I'd removed it the issue with this hook disappeared. I'm using the same format on hooks now without issue and I've made a note next to each in the file as a reminder to update both if I ever do change any part of either (!).
Thank you so much for walking me through this; it's been a valuable exercise and I think the hook is actually much better in the end too!