'How to update filefield_sources settings when adding new field through hook_form_alter in Drupal 7

I am trying to add a new field to an existing content type using form_alter, using below code I am able to have the field display on the form

function mymodule_form_alter(&$form, &$form_state, $form_id) {
   $form['new_product_field'] = array(
      '#type' => 'file',
      '#attributes' => array(
        'class' => array(
          'field-type-file',
          'field-widget-file-generic',
        ),
      ),
      '#title' => t('New product field'),
      '#description' => t('Description for this new product field'),
    );
}

On an existing field, I can run below code and that updates the below 3 settings.

But how do I get those same settings set on the field that I am creating above though the form_alter?

    $field_instance = field_info_instance('node', 'field_existing_field', 'resource');
    $field_instance['widget']['settings']['filefield_sources']['filefield_sources']['upload'] = 0;
    $field_instance['widget']['settings']['filefield_sources']['filefield_sources']['attach'] = "attach";
    $field_instance['widget']['settings']['filefield_sources']['source_reference']['autocomplete'] = 0;
    field_update_instance($field_instance);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source