• Resolved TomJ37

    (@tomj37)


    Hiya, file upload error reporting has been broken in every update since v1.17.2.
    Keeps saying no file, or failed to save file. The error is different in different versions.

    But the key part to getting the error to show up is a custom hook firing a certain function.

    I have a hook on “forminator_custom_form_submit_field_data”
    That hits a function to process the form in a way the Plugin can’t.

    So the form has a file upload for an image, and it also processes the image.
    If the hook fires any WordPress handle upload i.e. wp_handle_upload() or wp_handle_sideload() or media_handle_upload().

    The form errors out. And give a message that it failed to submit even though it did.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @tomj37

    I hope you’re well today!

    There has been some changes in plugin code (it’s expected over updates) and I think those may affect your custom code indeed.

    The upload itself is working but if there is any custom code involved, that code may need some slight adjustments.

    Would you, please, share your custom code (if there are any credentials included or other such “sensitive” data please strip or mask it) so we could take a look, test it, and see if and how we could help you modify it to make it work?

    Note: to share the code it’d be best to put it at pastebin.com and share direct link to it here instead of sharing code directly in post.

    Kind regards,
    Adam

    Thread Starter TomJ37

    (@tomj37)

    I gave you everything you need right there to re-produce.

    Hook in to:
    forminator_custom_form_submit_field_data

    then use any of the wp handle upload functions on the form i.e.

    wp_handle_upload()

    so as simple as this causes the error for me:

    
    add_filter('forminator_custom_form_submit_field_data','my_custom_function', 11, 2);
    
    functionn my_custom_function( $form_data, $form_id ) {
    //Loop and a Switch case to do stuff with form data. Here's upload only part.
    foreach($form_data as $k => $field) {
      switch($field['name']) {
      case 'upload-1':
    	if( isset( $field['value']['file'] ) && $field['value']['file']['error'] === 0 ) {
    	if( !empty($field['value']['file']['tmp_name']) ) {
    		$have_image = true;
    		$image = $field['value']['file'];
    	}
    }
    
    break;
      }
    }
    
    if( true === $have_image ) {
    //This will cause error						
    $upload = wp_handle_upload( $image, array('test_form' => false) );
    //Do other things
    }
    return $form_data;
    }
    

    I’ve even removed all other code and left 1 line to call wp_handle_upload on $form_data[‘upload-1’][‘value’][‘file’] = same result. If I don’t use a handle_upload function, no error from your form.

    Thanks

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @tomj37,

    Can you please confirm if you copied the same code that you are using? I can notice a syntax error in your code on a quick check. The spelling of “function” on the code you shared is wrong. Can you please correct the same and re-check if you still notice the issue?

    If the issue persists, please share the exact code with which you notice the problem so we can help you further.

    Kind Regards,
    Nebu John

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @tomj37 ,

    We haven’t heard from you for a while now, so it looks like you don’t have any more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

    Thread Starter TomJ37

    (@tomj37)

    Nope, just missed the last reply email notification.

    Yeah I typed most of that, as it was to show anexample, not actual code. You can do a 1 line function as mentioned to call wp_handle_upload, and it will bring the error.

    function my_hook() {
        wp_handle_upload($file, $args);
    }
    

    and get the result of breaking the forms.

    Just the 2 important factors to re-create, hook in to that hook and fire wp_handle_upload or any other handle_upload and it errors. That’s all you need to re-create.

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @tomj37

    I passed this query to our SLS Team so that they can dig into this issue further for you. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @tomj37,

    Forminator uses forminator_custom_form_submit_errors hook. Could you please check and see whether the following snippet helps:

    <?php
    
    add_filter('forminator_custom_form_submit_errors','my_custom_function', 11, 3);
    
    function my_custom_function( $submit_errors, $form_id, $form_data  ) {
    	//Loop and a Switch case to do stuff with form data. Here's upload only part.
    	foreach($form_data as $k => $field) {
    		switch($field['name']) {
    			case 'upload-1':
    				if( isset( $field['value']['file'] ) && $field['value']['file']['error'] === 0 ) {
    					if( !empty($field['value']['file']['tmp_name']) ) {
    						$have_image = true;
    						$image = $field['value']['file'];
    					}
    				}
    			break;
    		}
    	}
    
    	if( true === $have_image ) {
    		//This will cause error		
    		$upload = wp_handle_upload( $image, array('test_form' => false) );
    		//Do other things
    		foreach( $submit_errors as $error_id => $errors ){
    			unset( $submit_errors[ $error_id ][ 'upload-1' ] );
    			if( empty( $submit_errors[ $error_id ] ) ){
    				unset( $submit_errors[ $error_id ] );
    			}
    		}
    	}
    	return $submit_errors;
    }

    You can implement the above snippet as a mu-plugin. Please check this link on how to implement the above code as a mu-plugins:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Kind Regards,
    Nithin

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘File Upload – Broken after v 1.17.2 – Hook with handle_upload’ is closed to new replies.