如何保存一个图像文件(JPG和GIF)到数据库中?

王朝other·作者佚名  2008-05-18
宽屏版  字体: |||超大  

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();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有