Joomla 1.5 Bulk User Import CSV using PHP Script

While doing some work tonight, I came across a handy little script that is useful for importing users in bulk into Joomla using CSV files. This is very useful if your are moving user information from another content management system (CMS) into Joomla 1.5.

< ?php

// Hande form upload
if(isset($_POST['import'])) {

$mysql_host = trim($_POST['mysql_host']);
$mysql_user = trim($_POST['mysql_username']);
$mysql_password = trim($_POST['mysql_password']);
$mysql_schema = trim($_POST['mysql_schema']);

$table_prefix = trim($_POST['table_prefix']);

if(!mysql_connect($mysql_host, $mysql_user, $mysql_password) || !mysql_select_db($mysql_schema)) {
echo ‘Supplied MySQL details were incorrect - aborting’;
return;
}

// Get the joomla groups
$sql = sprintf(‘
SELECT  `id`, `value`
FROM    `%score_acl_aro_groups`
‘,
$table_prefix
);
$rs = mysql_query($sql);
$groups = array();
while($group = mysql_fetch_object($rs)) {
$groups[$group->value] = $group->id;
}

$fp = fopen($_FILES['csv']['tmp_name'], ‘r’);
while($user = fgetcsv($fp)) {

printf(‘Importing ”%s” … ’, $user[0]);

// Lookup and verify user group
if(!isset($groups[$user[4]])) {
printf(‘error: Invalid group (%s) for %s. Defaulting to Registered%s’, $user[4], $user[0], PHP_EOL);
$user[4] = ‘Registered’;
}

// Insert record into wsers
$sql = sprintf(‘
INSERT INTO `%susers`
SET
`name`            = ”%s”,
`username`        = ”%s”,
`email`           = ”%s”,
`password`        = ”%s”,
`usertype`        = ”%s”,
`block`           = ”%s”,
`sendEmail`       = ”%s”,
`gid`             = ”%s”,
`registerDate`    = NOW(),
`lastvisitDate`   = ”0000-00-00 00:00:00″,
`activation`      = ”",
`params`          = ”"
‘,
$table_prefix,
sql_prep($user[0]),
sql_prep($user[1]),
sql_prep($user[2]),
isset($_POST['md5_passwords']) ? md5($user[3]) : sql_prep($user[3]),
sql_prep($user[4]),
sql_prep($user[5]),
sql_prep($user[6]),
$groups[$user[4]]
);
mysql_query($sql);
// Get back ther user’s ID
list($user_id) = mysql_fetch_row(mysql_query(‘SELECT LAST_INSERT_ID()’));

// Insert record into core_acl_aro
$sql = sprintf(‘
INSERT INTO `%score_acl_aro`
SET
`section_value`   = ”users”,
`value`           = %d,
`name`            = ”%s”
‘,
$table_prefix,
$user_id,
sql_prep($user[0])
);
mysql_query($sql);

// Insert record into core_acl_groups_aro_map
$sql = sprintf(‘
INSERT INTO `%score_acl_groups_aro_map`
SET
`group_id`        = %d,
`aro_id`          = LAST_INSERT_ID()
‘,
$table_prefix,
$groups[$user[4]]
);
mysql_query($sql);

echo ‘done.’;
flush();
}

echo ‘

Done’;

} else {
// show upload form
?>

Import Users to Joomla

Use this script to do a bulk import of users into Joomla 1.5. Upload a CSV file with the following format: name, username, email, password, usertype, block, send_email Wrap details with commas in them in quotes.
CSV File:
MD5 Hash Passwords: *Check this option if the passwords in your CSV are in plain text
Joomla Table Prefix:
Joomla Database Name:
MySQL Host:
MySQL Username:
MySQL Password:
< ?php } function sql_prep($var) { return mysql_real_escape_string($var); } ?>



Share this:
  • RSS
  • PDF
  • Print
  • Slashdot
  • Facebook
  • LinkedIn
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • MySpace
  • StumbleUpon
  • Technorati
  • HackerNews

4 Responses to “Joomla 1.5 Bulk User Import CSV using PHP Script”


  1. 1 Scott Carroll

    That is a nice little snippet of code, I am going to have to borrow this for a project. I need to move a forum over to joomla and I am outputting a CSV for it. This is EXACTLY what I was looking for. I’ll let you know if it works out.

  2. 2 NickOg

    This worked out well for me. I had a few problems in copy and paste – odd things like ” coming out wrong. I have kept a copy of the final file I produced and included some progress output and so on. Is that of use to you as a downloadable copy?

    Thanks

    Nick

  3. 3 Matthew Cluver

    Hi Nick,

    That’s strange to hear about the “copy and paste” issues, sounds like a possible encoding issue. I would definitely welcome the file you created for offering a download link to everyone, you may email it over to support [at] piranhamethod.com.

    We found a really neat plugin the other day which we are going to start using on our blog soon, it was specifically designed for distributing code and maybe it will fare better for sharing code on the site.

    Best regards,

    Matt

  4. 4 Matthew Cluver

    Apparently there is a component that was created that can do this: http://extensions.joomla.org/extensions/migration-&-conversion/users-import-&-export/5430/details

    I added in a plugin for Wordpress that should make the code on the blog nice and pretty now as a well.

Leave a Reply

You must login to post a comment.