How to import many users

Last updated by zyon Comments (20)

Here is a small import script that worked for me.

 

csv file (utf-8 encoded):

"name";"username";"email";"plaintextpassword"

 

<?php
    require_once('/var/www/htdocs/engine/start.php');
    global $CONFIG;
    $ordner = "/var/www/htdocs";
    $rb_datei = fopen($ordner."/user.csv", "r");
    if ($rb_datei) {
        $array = explode("\n", fread($rb_datei, filesize($ordner."/user.csv")));
    } 
    $total_array = count($array);
    echo $total_array;   
    $i = 0;       
    while($i < $total_array) {           
        $data = explode(";", $array[$i]);       
        $name = str_replace('"','',utf8_decode($data[1]));
        $username = str_replace('"','',utf8_decode($data[2]));
        $email = str_replace('"','',utf8_decode($data[3]));       
        $password = str_replace('"','',utf8_decode($data[4]));   
        $guid = register_user($username, $password, $name, $email, false);
        $user = get_entity($guid);   
        $user->enable();
        set_email_validation_status($user->getGUID(), true);       
        echo "<br>Username: ".$username." - Name: ".$name. " - E-Mail: ".$email. " - Password: ".$password;           
        $i++;
    }   
?>

  • Hmn - Having a slight problem, it's running and displaying the number of records (168) but that's it... any ideas anyone?

  • Aha, it's obvously getting as far as this:

    <?php
        require_once('/var/www/htdocs/engine/start.php');
        global $CONFIG;
        $ordner = "/var/www/htdocs";
        $rb_datei = fopen($ordner."/user.csv", "r");
        if ($rb_datei) {
            $array = explode("\n", fread($rb_datei, filesize($ordner."/user.csv")));
        } 
        $total_array = count($array);
        echo $total_array;  

    Then stopping - anyone know why? Could it be to do with my mySQL settings? I think it's all set up properly...

  • Its import that usernames have a minimun of 4 and passwords have a minimum of 6 characters.

    Is there data in the array?

    <?php echo vardump($array); ?>

  • Ah, the data isn't getting into the array for some reason,  echo vardump($array);  returns nothing.

  • Also, interestingly enough, when I remove all but one record to try importing just that, it returns "2" rather than "1".

    I've found a couple of 3 letter usernames and removed them.

    Any more ideas?

  • Have been users imported? No Problem if it returns 2 or 1, modify the counter var :)

  • No, I'm afraid not - it's still not filling the array.

  • Here a line of my csv-file:

    "charly";"charly";"charly@xxx.de";"plaintextpassword"

  • Thanks, Here's 2 of mine:

    "Aidan Jackson";"aidanj";"aidanjackson@xxx.org";"Passw99"
    "Aisling Lyon";"aislingl";"aislinglyon@xxx.org";"Passw99"

    But sadly not working :(

  • Perhaps you can try:

    $array = explode("\n\r", fread($rb_datei, filesize($ordner."/user.csv")));

  • I run under Linux, perhaps you have a windows file

    For windows you need:

    $array = explode("\r\n", fread($rb_datei, filesize($ordner."/user.csv")));

    For MacOS:

    $array = explode("\r", fread($rb_datei, filesize($ordner."/user.csv")));

    For Linux:

    $array = explode("\n", fread($rb_datei, filesize($ordner."/user.csv")));

  • Or yout path is incorrect, try:

    require_once('/Your/path/to/ELGG-root/engine/start.php');
    $ordner = "/Your/path/to/ELGG-root";

  • Thanks for your help Zyon - I'm having some DNS issues at the moment, when I get through them I'll try these variations.

    The path is definately right though, thankfully!

  • Thanks for this helpful contribution. I am facing a slightly different situation. I would need to import about 3000 users BUT :

    My passwords are already hashed using MD5. So I would have a file like this

    name, username, email, md5/PASSWORD

    Any idea about a method I could get through to import those users into the elgg users_entity table ?

  • Ok .. I checked out with all my available neurones and finally I could get through.

    Here is the idea 

    i catch the username and password fields from my legacy login page, get the legacy email adress and  name from my existing table and create a new user dynamicaly using :

    $guid = register_user($username, $password, $name, $email, false);
            $user = get_entity($guid);    
            $user->enable();
            set_email_validation_status($user->getGUID(), true); 

    and so I can get my elgg table incremented when users log into the main login page of the site.

    This Elgg is really fantastic ... guess I will probably go further into it and produce a couple of plugin for Mobile phones use and extending the profile capabilities

    See U soon

  • I've found that the register_user method can be a bit flaky; especially when you are iterating over a lot of users (22K in my case). The problems was solved with above script, but tweaked a bit to actually directly invoke the user object methods: I replaced:

            $guid = register_user($username, $password, $name, $email, false);

    with

          try {
                    $user = new ElggUser();
                                 $user->username = $username;
                                 $user->email = $email;
                                 $user->name = $name;
                                 $user->access_id = ACCESS_PUBLIC;
                                 $user->salt = generate_random_cleartext_password(); // Note salt generated before password!
                                 $user->password = generate_user_password($user, $password);
                                 $user->owner_guid = 0; // Users aren't owned by anyone, even if they are admin created.
                                 $user->container_guid = 0; // Users aren't contained by anyone, even if they are admin created.
                                 $user->save();
                                $guid = $user->getGUID();
                }
                catch ( RegistrationException $e ){
                        echo $e;
                            $guid = 0;
                        }

     

    Which seemed to help....

  • I found an upload plugin (maybe it's based on this thread, I don't know) but plugin form might be the most user friendly format...

    http://community.elgg.org/pg/plugins/naakka/read/165550/upload-users

  • not working, no complaints and no users added, just number of 239 print out.

  • @Roman: this posting is very, very old. I would not even start looking for why it's not working.

    You might find this plugin useful: http://community.elgg.org/plugins/852868/1.8.1/csv-user-upload (though I don't know if this works either as I've not tested it myself - at least it says it's for Elgg 1.8).