Ok I have rewritten the file from scratch and this time tested it.BrennanU wrote:I dont mean to be rude, but I dont know much about php or phpbb. Could someone give me the exact code that I need to use to retrieve users from the csv once it is figured out?
Code: Select all
"User 01","pwd01","[email protected]",2,"en",0
"User 02","pwd02","[email protected]",2,"en",0
"User 03","pwd03","[email protected]",2,"en",0
Code: Select all
$users_data[] = array(
0 => 'username',
1 => 'password',
2 => 'email',
3 => 'group_id',
4 => 'lang',
5 => 'type',
);
Code: Select all
<?php
/**
*
* @package phpBB3
* @version $Id: users_add.php,v 1.0.0 2008/07/29 00:00:00 Mighty Gorgon Exp $
* @copyright (c) 2008 Mighty Gorgon
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
*/
/**
* @ignore
*/
@set_time_limit(0);
//@ignore_user_abort(true);
@ini_set('memory_limit', '64M');
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$users_start = request_var('us', 0);
$users_step = request_var('up', 50);
$users_data = array();
/*
$users_data[] = array(
'username' => 'username',
'user_password' => 'password',
'user_email' => 'email',
'group_id' => 2,
'user_lang' => 'en',
'user_type' => 0,
'user_regdate' => '',
);
$users_data[] = array(
0 => 'username',
1 => 'password',
2 => 'email',
3 => 'group_id',
4 => 'lang',
5 => 'type',
6 => 'regdate',
);
CSV Example:
"User 01","pwd01","[email protected]",2,"en",0
"User 02","pwd02","[email protected]",2,"en",0
"User 03","pwd03","[email protected]",2,"en",0
*/
$users_counter = 0;
$handle = fopen('users_to_add.csv', 'r');
while (($data = fgetcsv($handle, 0, ',')) !== false)
{
$num = count($data);
for ($c = 0; $c < $num; $c++)
{
$users_data[$users_counter][$c] = $data[$c];
}
$users_counter++;
}
fclose($handle);
//include($phpbb_root_path . 'users_to_add.' . $phpEx);
$users_list = '';
$total_users = count($users_data);
$users_this_step = min($users_start + $users_step, $total_users);
$users_this_step = ($users_this_step == 0) ? $total_users : $users_this_step;
$new_start = $users_start;
$users_fields_name = array('username', 'user_password', 'user_email', 'group_id', 'user_lang', 'user_type', 'user_regdate');
for ($i = $users_start; $i < $users_this_step; $i++)
{
$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], '2', 'en', '0', time());
for ($j = 0; $j < count($users_fields_name); $j++)
{
$users_data[$i][$j] = (empty($users_data[$i][$j])) ? $users_fields_values[$j] : $users_data[$i][$j];
$users_data[$i][$users_fields_name[$j]] = $users_fields_values[$j];
unset($users_data[$i][$j]);
}
$user_id = user_add($users_data[$i]);
if ($user_id !== false)
{
$users_list .= (($users_list == '') ? '' : ', ') . $users_data[$i]['username'];
}
$new_start++;
}
$message_text = 'The following users have been added to the DB:<br /><br />' . $users_list;
if ($new_start >= $total_users)
{
$message = 'All the users have been imported correctly!<br /><br />' . $message_text;
$template->assign_vars(array(
'MESSAGE_TITLE' => 'ADDING USERS',
'MESSAGE_TEXT' => $message
)
);
page_header('ADDING USERS');
$template->set_filenames(array('body' => 'message_body.html'));
page_footer();
}
else
{
$meta_url = append_sid("{$phpbb_root_path}users_add.$phpEx", "us=$new_start&up=$users_step");
meta_refresh(3, $meta_url);
$message = 'The process is not finished yet, this page will redirect automatically in few seconds, please wait...<br /><br />' . $message_text;
trigger_error($message);
exit;
}
?>
Code: Select all
while (($data = fgetcsv($handle, 0, ',')) !== false)
Code: Select all
while (($data = fgetcsv($handle, 200, ',')) !== false)
I set the length parameter to 200 (which is longer than my longest entry), and it worked fine.length
Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.0.4 and later) the maximum line length is not limited, which is slightly slower.
Mmm... that is strange... are you sure that the group exists and can accept new users?BrennanU wrote:All of the users add for me, the only thing is that whatever group id I input, it doesnt add the user to the group.
You are welcome... glad you solved.harley1979fxe wrote:Thank you very much! This helps a great deal!
That is because Excel escapes double quotes with double double quotes... you need to specify settings differently while exporting.harley1979fxe wrote:One other thing to note: When I export (save as) from Excel to .csv, " characters show up as """.
I guess if you have commas in fields you will have problems... it is always a good thing escaping special chars and strings.harley1979fxe wrote:I found that there is no need for the double quote around the text fields as it is interpreted correctly when called in with ftpgetcsv. Thanks again!
Code: Select all
$users_data[] = array(
'username' => 'username',
'user_password' => 'password',
'user_email' => 'email',
'group_id' => 2,
'user_lang' => 'en',
'user_type' => 0,
'user_regdate' => '',
);
Code: Select all
$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], '2', 'en', '0', time());
Code: Select all
$users_fields_values = array($users_data[$i][0], phpbb_hash($users_data[$i][1]), $users_data[$i][2], $users_data[$i][3], 'en', '0', time());
Code: Select all
while (($data = fgetcsv($handle, 0, ',')) !== false)
Code: Select all
while (($data = fgetcsv($handle, 100000, ',')) !== false)