Programatically Submitted Form With 'file' Field Throws Notice This seems to actually be a problem in Drupal itself. In /modules/file/file.field.inc the function file_field_widget_form has a line like this near the bottom (just before the return statement):

$elements['#file_upload_description'] = theme('file_upload_help', array('description' =&gt; '', 'upload_validators' =&gt; $elements[0]['#upload_validators']));


The issue is a small if condition a few lines back which looks like this:

if (($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta &lt; $field['cardinality']) &amp;&amp; empty($form_state['programmed'])) {
    $elements[$delta] = $element;
    ...
    some more stuff
}


Since I am submitting my form programatically with drupal_form_submit, it adds in $form_state['programmed'], thus the if condition fails, $elements[0] does not exist and I get the error

Notice: Undefined offset: 0 in file_field_widget_form()


My form is submitted by a custom routine (in a custom module) that looks a bit like this:

foreach ($field_data as $k =&gt; $detail) {
    $field_key = _get_field_keys('field_' . $k);
    $field_key = $field_key[0];

    $node -&gt; {'field_' . $k}[$node -&gt; language][0][$field_key] = $detail;
    $form_state['values']['field_' . $k] = $detail;   
}

drupal_form_submit("{$node-&gt;type}_node_form", $form_state, $node);


The form state before I call drupal_form_submit looks like this:

Array
(
    [values] =&gt; Array
        (
            [title] =&gt; some title
            [name] =&gt; admin
            [op] =&gt; Save
            [field_person] =&gt; 88
            [field_lead_type] =&gt; 1
            [field_lead_details] =&gt; some stuff
    )


)

What is the fix? I'd rather not mess with core modules. Is there something I can do to my own code to resolve the problem?
ok, why are you not submitting a File (or FID) along with your form_state? I don't understand your problem, because I do this all the time (even with files).

Drupals file widgets usually use like Ajax to upload a file in the background and the form takes a FID in $form_state to process your forms if it has files attached.

When I asked you what the $form_state was I meant:


what are you sending to drupal.
what does drupal typically send as the $form_state when that form is sent through the user interface, because you're just trying to do that programmatically.


I dont think you know the answer to (2) above, because you cant get this to work right. You showed me what you're sending for (1). Here is a tutorial on using Firefox's Firebug plugin to view the submitted POST form state values for a drupal form. You want to drupal_form_submit form state to have the exact same parameters passed to it. In firebug you simply press Persist in the Net tab and you can capture URL requests.

So I'm saying goto the form in the website and submit it using your browser and Firebug to see what it expects to be passed.

You may ask how you get a FID for a file, you simply upload it using php before submitting the form and pass along its $file-&gt;fid. See this community documentation comment here how to do that.

You're just posting a simple HTTP Form, I really doubt Drupal is doing anything wrong.