MyBB Community Forums

Full Version: Import users through a file.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a problem, i am trying to import users through a file. Using mydevel_generate as an example. But i cant seem to be able to get the users to be added to the database. Even though there are no errors or problems.

Maybe somebody is able to see what is wrong with the following code:

function MyCSV_fileReader()
{
    global $db, $mybb;
    require_once MYBB_ROOT."inc/datahandlers/user.php";

    $CSV_url = $mybb->settings['mycsv_file'];
    $CSV_url_check = array('http','www.','https');

    if( ($CSV_url == "") or (MyCSV_check($CSV_url_check, $CSV_url)) )
    {
        flash_message(
            'The MyCSV importer failed to import the file.<br />
            This is due to a imporper URL make sure you use absolute paths and not URLs !'
        );
    }
    $counter = 0;
    $file_handle = fopen($CSV_url, "r");
    if ($file_handle == false) 
    {
        flash_message(
            'The MyCSV importer failed to import the file.<br />
            This is due to the fact that the file specified does not exist, or is not in the specified location !'
        );
    }
    else
    {
        while (!feof($file_handle)) 
        {
            $CSV_line = fgetcsv($file_handle);
            $counter++;

            if(strpos($CSV_line[0], "Username;") !== false)
            {
                continue;
            }
            $CSV_data = explode(";",$CSV_line[0]);
            $CSV_username = $CSV_data[0];
            $CSV_password = $CSV_data[1];
            $CSV_email = $CSV_data[2];

            $user = array(
                "username" => $CSV_username,
                "password" => $CSV_password,
                "password2" => $CSV_password,
                "email" => $CSV_email,
                "email2" => $CSV_email,
                "usergroup" => $mybb->settings['mycsv_usergroup'], 
                "displaygroup" => $mybb->settings['mycsv_usergroup'],
            );
            $userhandler = new UserDataHandler('insert');
            $userhandler->set_data($user);
            if($userhandler->validate_user())
            {
                $userhandler->insert_user();
                var_dump($userhandler);
            }
            else
            {
                
                $errors = $userhandler->get_friendly_errors();
                flash_message("An error has occure with the following user: {$username}.");
            }
            
        }
        fclose($file_handle);
        flash_message("All the: {$counter} users have been succesfully imported from the CSV file");
    }
}

Using this url: index.php?module=config&mycsv_import=1 everything works except for the fact that users are not being added. Maybe i am overlooking something incredibly simple. Any help would be greatly appreciated.
Still wondering if anybody would have a solution to this problem. Its probably something really simple i am missing.