如何保存一个图像文件(JPG和GIF)到数据库中? |
|
file: test_insert_jpg.pl
------------------------- #! /usr/bin/perl use DBI; open(IN,"/imgdir/bird.jpg"); $gfx_file=join('',); close(IN); $database="speedy"; $table="archive"; $user="stephen"; $password="none"; $dsn="DBI:mysql:$database"; $dbh=DBI->connect($dsn, $user, $password); $sql_statement=<<"__EOS__"; insert into $table (id, date, category, caption, content, picture1, picture2, picture3, picture4, picture5, source, _show) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) __EOS__ # uncomment to debug sql statement # -------------------------------- #open(SQLLOG,">>sql_log_file"); #print SQLLOG scalar(localtime)."\t$sql_statement\n"; #close(SQLLOG); $sth=$dbh->prepare($sql_statement); $sth->execute(NULL,NULL,"car|sports","Porsche Boxster S","German excellence",$gfx_file,NULL,NULL,NULL,NULL,"European Car","Y"); $sth->finish(); $sth=$dbh->prepare("SELECT * FROM $table"); $sth->execute(); while($ref=$sth->fetchrow_hashref()){ print "id = $ref->{'id'}\tcategory = $ref->{'category'}\tcaption = $ref->{'caption'}\n"; } $numRows=$sth->rows; $sth->finish(); $dbh->disconnect(); file: serve_gfx.cgi ----------------------------------------------------- #!/usr/bin/perl $|=1; use DBI; $database="speedy"; $table="archive"; $user="stephen"; $password="none"; $dsn="DBI:mysql:$database"; $dbh=DBI->connect($dsn, $user,$password); $sth=$dbh->prepare("select * from $table where id=1"); $sth->execute(); $ref=$sth->fetchrow_hashref(); print "content-type: image/jpg\n\n"; print $ref->{'picture1'}; $numRows=$sth->rows; $sth->finish(); $dbh->disconnect(); |