Drupal 7 Batch User Import script


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. $users = "
  5. first_name last_name\temail\tpassword\n
  6. first_name last_name\temail\tpassword\n
  7. first_name last_name\temail\tpassword\n
  8. first_name last_name\temail\tpassword\n
  9. first_name last_name\temail\tpassword\n
  10. ";
  11.  
  12. define('DRUPAL_ROOT', getcwd());
  13. $_SERVER['HTTP_HOST'] = 'default';
  14. $_SERVER['PHP_SELF'] = '/index.php';
  15. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  16. $_SERVER['SERVER_SOFTWARE'] = NULL;
  17. $_SERVER['REQUEST_METHOD'] = 'GET';
  18. $_SERVER['QUERY_STRING'] = '';
  19. $_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
  20. $_SERVER['HTTP_USER_AGENT'] = 'console';
  21.  
  22. include_once DRUPAL_ROOT . '/includes/password.inc';
  23. include_once DRUPAL_ROOT . './includes/bootstrap.inc';
  24. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  25.  
  26.  
  27. $users = explode("\n", $users);
  28. foreach ($users as $user)
  29. {
  30. if (empty($user))
  31. {
  32. continue;
  33. }
  34.  
  35. $user = explode("\t", $user);
  36. if (count($user) != 3)
  37. {
  38. continue;
  39. }
  40.  
  41. $old = user_load_by_mail($user[1]);
  42. if (!empty($old) && $old->uid)
  43. {
  44. continue;
  45. }
  46.  
  47. $obj = new stdClass();
  48. $obj->name = trim($user[0]);
  49. $obj->pass = user_hash_password(trim($user[2]));
  50. $obj->mail = trim($user[1]);
  51. $obj->theme = 'garland';
  52. $obj->status = 1;
  53. $obj->roles = array(
  54. 2 => 'authenticated user',
  55. // here is the role list
  56. // [ROLE_ID] => '[ROLE_NAME]'
  57. );
  58.  
  59. user_save($obj);
  60. }
  61.  
  62. echo 'Done';

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.