posted July 23, 2000 01:49 PM
[QUOTE]Originally posted by dis:
[B]Does any know where I can get an image
into a blob type column from a perl dbi script?
tried the following substitutions with no success.$var=~s/\'/\\\'/g;
$var=~s/\\/\\\\/g;
$var is where the image is located the
insert always barfs
any suggestions how to get it in?
Thanks in advance,
Rick
rick@erealestateforyou.com
Here is my way to solve this problem, by using an filehandler $ICONFILE
my $icon;
my $filesize;
if ($ICONFILE ne '') {
$filesize = -s $ICONFILE;
my $in = read($ICONFILE, $icon, $filesize,0);
if ($in != $filesize) {
error("Error reading the file $ICONFILE");
}
$icon =~ s/\\/\\\\/g; # first do this
$icon =~ s/\000/\\0/g; # change NUL
$icon =~ s/\'/\\\'/g;
$icon =~ s/\"/\\\"/g;
}
Now you can use $icon within the sql-statement like:
$sql = qq{INSERT INTO images VALUES ("$id", "$mimetype","$icon")};
To get the image out of the database i wrote this perl-script:
#!/usr/bin/perl -w
use strict;
use DBI;
use CGI qw/:standard/;
require "database.sub";
# in database.sub there are the functions to connect (Connact_DB), run sql-statements (Do_SQL) and to disconnect the database.
my $user = 'test';
my $passwd ='none';
my $id = param('id');
&Connect_DB($user,$passwd);
my $sql = "SELECT icon,mimetype FROM artikel WHERE id = $id";
my $sth = &Do_SQL($sql);
my $file = $sth->fetchrow_hashref;
my $icon = $file->{'icon'};
my $mimetype = $file->{'mimetype'};
$sth->finish;
&Disconnect;
print header('$mimetype');
print $icon;
exit(0);
#end
I'll hope it will help you.
Sauli