Drupal: Upload a file to insert the content into a database without storing the file on the server

This example is for the Drupal 6 CMS. The task was to upload a file and insert the content of the file into a database without permanently storing the file on the server. Normally, you would use a normal file form and read the content of the uploaded file. To make it working you have to implement a few important steps.

First, add the enctype into the _form hook.

function example_upload_form($form_state) {
  $form = array();

  // add the enctype
  $form['#attributes'] = array('enctype' => "multipart/form-data");

  [...]

    code

  [...]

  //
  // File Upload
  //
  $form['sourcefile'] = array(
    '#type' => 'fieldset',
    '#title' => t('File upload'),  
  );
  $form['sourcefile']['upload'] = array(
    '#type' => 'file',
    '#title' => t('Select your file'),
    '#description' => t('description'),
    '#size' => 40,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Start'),
  );
  return $form;
}

The _submit hook upload the file with the help of the file_save_upload. The uploaded file is only temporary added to the files table.

function example_upload_form_submit($form, &$form_state) {
  $file = file_save_upload('upload');

  // do something with the file
  example_insert_file($file);
}

Have fun.