Skip Navigation

[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, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2612911

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2612949

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

#2613033

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

#2613065

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;
        }
      }
    });  
#2613287

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2613703

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?

#2613769

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2614175
92ca8192-a984-4a6f-88b5-677403bdec83.png

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?

#2614219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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

#2614323

HI,

I didn't do the validation in the screenshot, it was already integrated

#2614349

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

For any additional validation that should be custom you will have to add form filter hook: cred_form_validate as shared before.