I am having a bit of a problem uploading files using drupals 4.7 form API. My objective is up put the file uploaded in the 'media' field in a directory, and store a link in a database. This works fine using the drupal upload Api, unless I am using the form API.

A sample of my code is below.
##################

// Form declaration:
  $form['save_promo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Edit or Create a Promotion.'),
    '#tree' => TRUE,
  );

// Field Declaration:

  $form['save_promo']['media_inline']['media_label'] = array(
    '#value' => "Media Attachment: ",
  );

  $form['save_promo']['media_inline']['media'] = array(
    '#type' => 'file',
    '#size' => 20,
    '#attributes' => array('style' => 'width:auto'),
  );

// Submit:

  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  $output = drupal_get_form('save_promo', $form);
  return $output;

// Uploading should look something like:
$file = file_check_upload('media');
file_save_upload('media', variable_get('file_directory_path', NULL));

Note:
The last section is just a sample, I have tried passing everything I could imagine, and have successfully been able to retreive the
filename itself with this:

$mediaAttachment = $form_values['save_promo']['media_inline']['media'];
echo $mediaAttachment returns the filename (not the tmp_name).

Comments

SubWolf’s picture

See Heine's example at http://drupal.pastebin.us/6084

The lack of...

$form['#attributes']['enctype'] = 'multipart/form-data';

Is prolly a part of the problem.