#!/usr/bin/perl -w
# FILE:  upload_image.pl
# Uploads gif images into database through a Perl Script.

use DBI;

my $image_data;
my $data='';  #buffer to hold image info

print "Name of file to upload: ";
my $image_file = <STDIN>;
chomp($image_file);
print "Team ID Number: ";
my $teamID = <STDIN>;
chomp($teamID);

my $mime = "image/gif";
my $db = 'NCAA';
my $server = 'hollerith';
my $length = 0;

open(FH, "$image_file") or die "Could not open file.";
binmode(FH);
while(read(FH, $data, 1024)  )
{
  $image_data .= $data;
}
close(FH);
die "image is empty" unless ($image_data);

my $dbh = DBI->connect("dbi:mysql:$db:$server", "tgarrett", "blackadder")
  or die("Error! $!\nAborting");

$image_data = $dbh->quote($image_data);
$mime = $dbh->quote($mime);

my $sql = qq{INSERT INTO Logo (teamID,image,mime)
  VALUES (?, $image_data, "image/gif") };
my $sth = $dbh->prepare($sql) or die "could not prepare";
$sth->execute($teamID) 
  or die ("Error:  $DBI::errstr \nAborting");
print "Image file: $image_file of Type: $mime was successfully loaded into the database $db"; 
