View Full Version : [ Release v2.0.1 ] Avatar import from filesystem

19 Jun 2001, 19:42
Me again - another simple integration hack for those moving from something like UBB. In my old hacked up UBB, I had my own avatar hack (not one of the common ones floating around). At any rate, all the avatars were stored in a single directory, and they all were named "username.gif", where "username" was the account name (bob.gif, dumbass.gif, etc.). If you have avatars for folks who have spaces in their username, and you've replaced them with underscores (john_doe.gif), this script will handle that, too.

Backup your customavatar table first. If you hose it with this script, don't cry to me....you've been warned. :p

This is a perl script - to be run from the directory containing your avatars:


# Must be run *from* the avatar directory

# Configuration section
$avatardir = ".";

use DBI;


$dbh = DBI->connect( $datasource, 'id', 'password');

if ( !defined $dbh ) {
die "Cannot connect to mySQL server: $DBI::errstr\n";

my @filearray = glob("$tinydir/*.gif");

foreach $filename (@filearray)
$filename =~ s/\.\///g;
$username = $filename;
$username =~ s/_/ /g;
$username =~ s/\.gif//g;

# Okay, grab the user ID from the database that matches this user
$quoted_username = $dbh->quote($username);
$sth = $dbh->prepare("SELECT userid FROM user WHERE username = $quoted_username");

@result = $sth->fetchrow_array;

$user_id = $result[0];

if($user_id == "") {
print "Invalid account name: [$user_id] $quoted_username (skipping)\n";

print "Processing --> [$user_id] $username ($filename)\n";


# Prepare file for binary insertion
open(IMG, "<$filename");
read(IMG, $filestuff, 100000);

$quoted_bindata = $dbh->quote($filestuff);
$quoted_filename = $dbh->quote($filename);

# Grab current time
$currtime = time();

# Insert the user's original avatar into the customavatar table
$q = "INSERT into customavatar (userid,avatardata,dateline,filename) VALUES ($user_id, $quoted_bindata, $currtime, $quoted_filenam
$sth = $dbh->prepare($q);

$sth->execute || print "ERROR INSERTING ROW!\n";



Paste this into a script, chmod 755, and run it from the directory containing your avatars.

You may need to play with a bit to work with your directory of avatars - every hacked-up UBB (or whatever) is different. But it should be a good start at least ...



19 Jun 2001, 22:48
Nice easy hack..

I will try it..