Skip to content

Instantly share code, notes, and snippets.

@xu-li
Created July 6, 2012 07:56
Show Gist options
  • Save xu-li/3058808 to your computer and use it in GitHub Desktop.
Save xu-li/3058808 to your computer and use it in GitHub Desktop.
A drupal7 script for batch importing users
#!/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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment