Return to Snippet

Revision: 58298
at July 6, 2012 17:42 by athenalightened


Initial Code
#!/usr/bin/php
<?php

$users = "
  first_name last_name\temail\tpassword\n
  first_name last_name\temail\tpassword\n
  first_name last_name\temail\tpassword\n
  first_name last_name\temail\tpassword\n
  first_name last_name\temail\tpassword\n
";

define('DRUPAL_ROOT', getcwd());
$_SERVER['HTTP_HOST']       = 'default';
$_SERVER['PHP_SELF']        = '/index.php';
$_SERVER['REMOTE_ADDR']     = '127.0.0.1';
$_SERVER['SERVER_SOFTWARE'] = NULL;
$_SERVER['REQUEST_METHOD']  = 'GET';
$_SERVER['QUERY_STRING']    = '';
$_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
$_SERVER['HTTP_USER_AGENT'] = 'console';

include_once DRUPAL_ROOT . '/includes/password.inc';
include_once DRUPAL_ROOT . './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);


$users = explode("\n", $users);
foreach ($users as $user)
{
  if (empty($user))
  {
    continue;
  }
  
  $user = explode("\t", $user);
  if (count($user) != 3)
  {
    continue;
  }
  
  $old = user_load_by_mail($user[1]);
  if (!empty($old) && $old->uid)
  {
    continue;
  }
  
  $obj = new stdClass();
  $obj->name = trim($user[0]);
  $obj->pass = user_hash_password(trim($user[2]));
  $obj->mail = trim($user[1]);
  $obj->theme = 'garland';
  $obj->status = 1;
  $obj->roles = array(
    2 => 'authenticated user',
    // here is the role list
    // [ROLE_ID] => '[ROLE_NAME]'
  );
  
  user_save($obj);
}

echo 'Done';

Initial URL

                                

Initial Description
# HOW TO USE #
1. Copy & save this into scripts folder.
2. cd YOUR-DRUPAL-ROOT-PATH
3. php scripts/YOUR-SCRIPT-FILE

Initial Title
Drupal 7 Batch User Import script

Initial Tags

                                

Initial Language
PHP