How to programmatically handle files (CSV import) I'm trying to make a simple form to upload a txt/csv file, in order to parse it and update some values in database. Here's my code. Unfortunately, at the moment using the form nothing happens (no folder creation, no file upload...) Here's the code. What can be wrong here? Thank you

function vts_departs_conf_form() {
  $form = array();

  $form['browser'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('Send file'),
    '#collapsible' =&gt; TRUE,
    '#description' =&gt; t("Send foo file."),
  );

  $file_size = t('Maximum file size: !size MB.', array('!size' =&gt; file_upload_max_size()));

  $form['browser']['file_upload'] = array(
    '#type' =&gt; 'file',
    '#title' =&gt; t('CSV File'),
    '#size' =&gt; 40,
    '#description' =&gt; t('Select the CSV file to be imported. ') . $file_size,
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit', 
    '#value' =&gt; t('Save'),
  );

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

  return $form;
}

function vts_departs_conf_form_validate($form, &amp;$form_state) {
  // attempt to save the uploaded file
  $filepath = 'public://depconfs';
  file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);
  $file = file_save_upload('file_upload', array('file_validate_extensions' =&gt; array('CSV TXT')), $filepath, FILE_EXISTS_REPLACE);

  // check file uploaded OK
  if (!$file) { 
    form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.'));
  }
  else {
    // set files to form_state, to process when form is submitted
    $form_state['values']['file_upload'] = $file;
  }
}

function vts_departs_conf_form_submit($form, &amp;$form_state) {
  //Prepare file parsing
  $line_max = variable_get('user_import_line_max', 1000);
  ini_set('auto_detect_line_endings', true);
  $filepath = $form_state['values']['file_upload']-&gt;filepath;
  $handle = @fopen($filepath, "r");

  while ($row = fgetcsv($handle, $line_max, ',')) {
    // do stuff
  }
}


EDIT :

As a first step, @bdurbin corrected my naming mistakes. 

Reading file_save_upload() API description, I realised that my basic validation checks are already performed, so I removed them. 

So final is quite lighter :

function vts_departs_conf_form_validate($form, &amp;$form_state) {

  $filepath = 'public://depconf/';
  $validators = array('file_validate_extensions' =&gt; array('csv txt'));
  $file = file_save_upload('file_upload', $validators , $filepath, FILE_EXISTS_REPLACE);
  //push file object to submit step 
  $form_state['values']['file_upload'] = $file;

}
Drupal's naming convention for the automagic association of validation and submit handlers for a given form is:


name_of_form() (builder)
name_of_form_validate() (validation handler)
name_of_form_submit() (submit handler)


In your case, if you wanted to name your form vts_departs_conf_form(), you'd want to either rename your validation handler to vts_departs_conf_form_validate() and your submit handler to vts_departs_conf_form_submit(), or use the #validate and #submit form attributes to associate functions that don't follow the naming conventions.

In your example, changing the builder name to vts_departs_conf() made the form work as expected.