Home › Toolset Professional Support › [Resolved] Empty upload file generate error
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
This topic contains 25 replies, has 2 voices.
Last updated by Minesh 1 year, 6 months ago.
Assisted by: Minesh.
I try to switch to media uploader but then when I try to upload the zero byte file I see the following error which you already know of:
File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini file or by post_max_size being defined as smaller than upload_max_filesize in php.ini.
It seems WordPress does not allow to upload the zero byte file itself.
HI,
yes, exactly with the wordpress media uploader this error is handled in that way however I use the form to let non-logged in visitor users enter data and media and for this reason I cannot use it (media uploader)
unfortunately it generates this error with email notifications activated, I don't understand the difference how the problem is handled when they are not activated and when they are
As it seems that even WordPress do not allow zero byte file upload.
To handle the error, you can add the following filter code to "Custom code" section offered by Toolset:
add_filter( 'cred_form_validate', 'ts_validate_form', 10, 2 ); function ts_validate_form( $error_fields, $form_data ){ $forms = array( 999999 ); // array of form ids to apply this code to $file_field_slug = 'file-field'; if ( in_array( $form_data['id'], $forms ) ) { list( $fields, $errors ) = $error_fields; if ( isset($fields["wpcf-{$file_field_slug}"]['file_data']['size'] ) && $fields["wpcf-{$file_field_slug}"]['file_data']['size'] == 0 ) { // problem, the filesize is zero $errors["wpcf-{$file_field_slug}"] = "You cannot upload an empty file (0 bytes)"; } $error_fields = array( $fields, $errors ); } return $error_fields; }
Where:
- Replace 999999 with your original form ID.
- Replace $file_field_slug value with your original field slug.
Hi,
The filter doesn't work it keeps giving me the fatal error, momentarily I set this jquery code to block the form if a file with weight equal to 0 is loaded
var fileInputs = $("input[type=file]"); var isValidLWF = true; $('.allegati-error').hide(); fileInputs.each(function() { var fileInput = this; if (fileInput.files.length > 0) { var fileSize = fileInput.files[0].size; if (fileSize === 0) { isValidLWF = false; $('.allegati-error').show(); return false; } } });
I wonder why the code does not work.
Can you please share problem URL where I can see the issue again and share admin access and tell me where exactly you added the code snippet I shared.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
HI,
At the moment I can't share these logins because the site is not under my management, I added the following code:
add_filter( 'cred_form_validate', 'ts_validate_form', 10, 2 ); function ts_validate_form( $error_fields, $form_data ){ $forms = array( 302 ); // array of form ids to apply this code to $file_field_slug = 'allegati'; if ( in_array( $form_data['id'], $forms ) ) { list( $fields, $errors ) = $error_fields; if ( isset($fields["wpcf-{$file_field_slug}"]['file_data']['size'] ) && $fields["wpcf-{$file_field_slug}"]['file_data']['size'] == 0 ) { // problem, the filesize is zero $errors["wpcf-{$file_field_slug}"] = "You cannot upload an empty file (0 bytes)"; } $error_fields = array( $fields, $errors ); } return $error_fields; }
Do you have any updates on the php 8 fatal error issue?
I've no update as I already shared workaround that should work. Regarding PHP 8 fatal error it may take long time as WordPress itself seems not allowing the zero byte file upload. For now you have to use workaround we shared.
HI,
Thanks a lot for the answer!
I have a question! I attached screenshot where the check is made when a file is loaded, in this case the file exceeds the allowed weight limit, can it also be done for the file that does not exceed a minimum weight?
Yes - you can but I'm not sure how you added the validation for now. Is it using the Toolset form hook cred_form_validate?
Please check the following related ticket that may help you:
- https://toolset.com/forums/topic/restricting-file-size-and-types-on-file-upload-cred-form/#post-1411409
HI,
I didn't do the validation in the screenshot, it was already integrated
For any additional validation that should be custom you will have to add form filter hook: cred_form_validate as shared before.