Handling file in Drupal
2011 10 Sep

Handling files with Drupal (CSV import)

Drupal allows pretty smooth file management API for developers. Here is a small piece of code where I tried to import the contents of an uploaded CSV file into the database in Drupal.

  1. Create a menu item for your file upload form
  2. Define the callback for the menu item and make sure to set the form encoding
  3. Make sure that the file imported is of CSV type (check the mime type of the file uploaded)
  4. And finally, get data from the CSV And TA-DA :-)

 

  1. Create a menu item for your file upload form
    function example_menu() { 
    $items = array(); 
    $items['admin/example/import'] = array(
      'title' => 'Import', 
      'page callback' => 'drupal_get_form', 
      'page arguments' => array('example_form'), 
      'access arguments' => array('allow example import'), 
      'type' => MENU_LOCAL_TASK, 
    ); 
    return $items; 
    }

     

  2. Define the callback for the menu item and make sure to set the form encoding
    function example_form() { 
    
      $form = array(); 
      $form['browser'] = array(
        '#type' => 'fieldset', 
        '#title' => t('Browser Upload'), 
        '#collapsible' => TRUE, 
        '#description' => t("Upload a CSV file."), 
      ); 
      $file_size = t('Maximum file size: !size MB.', array('!size' => file_upload_max_size()));
      $form['browser']['file_upload'] = array( 
        '#type' => 'file', 
        '#title' => t('CSV File'), 
        '#size' => 40, 
        '#description' => t('Select the CSV file to be imported. ') . $file_size
      ); 
      $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); 
      // set the form encoding type 
      $form['#attributes']['enctype'] = "multipart/form-data"; 
      return $form; 
    }

     

  3. Make sure that the file imported is of CSV type (check the mime type of the file uploaded)
    function example_form_validate($form, &$form_state) { 
      // attempt to save the uploaded file 
      $file = file_save_upload('file_upload'); 
      // check file uploaded OK 
      if (!$file) { 
        form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.')); 
      } 
      else if($file->filemime != 'text/csv') { 
        form_set_error('file_upload', t('The file must be of CSV type only.')); 
      } 
      else { 
      // set files to form_state, to process when form is submitted 
        $form_state['values']['file_upload'] = $file; 
      } 
    }

     

  4. And finally, get data from the CSV
    function example_form_submit($form, &$form_state) {
      $line_max = variable_get('user_import_line_max', 1000);
      ini_set('auto_detect_line_endings', true);
      $filepath = $form_state['values']['file_upload']->filepath;
      $handle = @fopen($filepath, "r");
      // start count of imports for this upload
      $send_counter = 0;
      while ($row = fgetcsv($handle, $line_max, ',')) {
      // $row is an array of elements in each row
      // e.g. if the first column is the email address of the user, try something like
        $mail = $row[0];
      }
    }

     

And TA-DA :-) This is a very simple example of how to write code to import content using CSV. File handling APIs are much better in Drupal 7, will post about the same when we get a chance.

Latest Blogs

UX/UI Synergy: Harmonizing Design for Success

The UX/UI Synergy: Harmonizing Design for Success

In the development and designing world, both UX and UI are the most commonly used terms for web and app designing.

Read More

Smart Website Design Fuels Business Growth

How Smart Website Design Fuels Business Growth ?

No doubt! With many people using the internet for business and information, owning a website has become more important than ever.

Read More

Top Trends in iOS Development Languages

Top Trends in iOS Development Languages: What's Next?

iOS development is always an exciting field; however, with millions of applications in the Apple Store, it is reaching an all-time high.

Read More

how to design a website

How to Design a Website: 8 Steps to Creating an Effective Site Design

In this day and age, having a website for a business is crucial to succeed.

Read More