This support ticket is created Il y a 6 années et 3 mois. 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.
Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.
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);
}
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.
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':
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:-
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';
}
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);
}
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.
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!