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

Scalable Drupal Development Services for Growing Enterprises

Scalable Drupal Development Services for Growing Enterprises

The right content management system (CMS) can make the difference between your website project being an enormous achievement and a complete failure when you're ready to develop one.

Read More

Best ReactJS UI Frameworks for Stunning User Interface

ReactJS UI Frameworks to Create Stunning User Interfaces

A set of components that may be used in React applications is called a ReactJS UI framework or a React component library.

Read More

Improve SEO of Ecommerce Website for Higher Ranking

Cracking the Code: SEO for Ecommerce Websites

For e-commerce websites, search engine optimization is low-hanging fruit. Many online stores are built without regard for search engines.

Read More

create responsive website using flexbox

How to Create a Responsive Website Using Flexbox

In the modern era of technology, businesses and individuals must have a website that can respond to various screen sizes.

Read More