如何使用PHP向数据库中插入图片,,并且使得图片可以显示在页面上??

2024-12-03 12:59:52
推荐回答(3个)
回答1:

一般不向数据库插入图片 而是插入图片的src 通过src找到图片然后显示。
session_start();

//array数组中放图片的格式
$uptypes = array("image/jpg","image/jpeg","image/png","image/pjpeg","image/gif","image/bmp","image/x-png");
$files =$_FILES["uppic"];
if($files["size"]>2097152){ //图片大小判断
echo "上传图片不能大于2M";
echo "";
exit;
}
$ftype =$files["type"];
if(!in_array($ftype,$uptypes)){ //图片格式判断
echo "上传的图片文件格式不正确";
echo "";
}
$fname = $files["tmp_name"]; //在服务器临时存储名称
$image_info = getimagesize($fname);
$name = $files["name"];
$str_name = pathinfo($name); //以数组的形式返回文件路劲的信息
$extname = strtolower($str_name["extension"]); //把字符串改为小写 extensiorn扩展名
$upload_dir = "upload/"; //upload文件夹
$file_name = date("YmdHis").rand(1000,9999).".".$extname;
$str_file = $upload_dir.$file_name; //文件目录
//存入数据库
$con=mysql_connect("localhost","root","");
if(!$con){
die(("数据库连接失败").mysql_error());
}
mysql_select_db("mywork",$con);
$sql="update user set picpath='$str_file' where user_name='$username'"; //将图片地址插入数据库mywork
mysql_query($sql,$con);
mysql_close($con);
if(!file_exists($upload_dir)){
mkdir($upload_dir); //创建目录 成功则返回true 失败则返回flase
}
if(!move_uploaded_file($files["tmp_name"],$str_file)){ //将上传的文件移动到新的目录 要移动文件 和文件新目录 成功则返回true
echo "图片上传失败";
echo "";
}
else{
//echo "";
echo "图片上传成功";
echo "";
}

回答2:

数据库倒是可以插入图片,但那样的话,你的数据库很快就满了,并且效率也很低,所以没人向数据库里插入图片,但是可以插入图片的SRC,这样,把图片传到你的空间中,就可以通过设置SRC来显示图片了,明白我的意思吗?
鲜少老师 www.itcyly.com

回答3:

procedure Txt_bjtpwh_f.FormCreate(Sender: TObject);
begin
q_sel.Close;
q_sel.SQL.Text:='select * from bjtxsjb where x_xh in (select min(x_xh) from bjtxsjb)';
q_sel.Open;
if q_sel.eof then exit;
f_showjpg;
end;

procedure Txt_bjtpwh_f.s_addClick(Sender: TObject);
begin
q_sel.Append;
image1.Picture.bitmap.Assign(nil);
s_open.Click;
end;

procedure Txt_bjtpwh_f.s_openClick(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
if not FileExists(OpenPictureDialog1.FileName) then exit;
image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
if not (q_sel.State in [dsedit,dsinsert]) then
q_sel.edit;
end;
end;

procedure Txt_bjtpwh_f.s_saveClick(Sender: TObject);
var JpgField:TBlobField; stre:tmemorystream;
begin
if not (q_sel.State in [dsedit,dsinsert]) then exit;
stre:=tmemorystream.Create;
try
JpgField:=(q_sel.FieldByName('x_tx') as TBlobField);
image1.Picture.Graphic.SaveToStream(stre);
jpgfield.LoadFromStream(stre);
finally stre.free; end;
q_sel.FieldByName('x_rq').AsDateTime:=u_GetDbDate;
q_sel.Post;
end;

function u_GetDbDate:TDate;
var l_n,l_y,l_r:word;
begin
qry.Connection:=main_f.ADOConnection1;
qry.Active:=false;
qry.SQL.Clear;
qry.SQL.Add('select getdate() as dbdate');
qry.Active:=true;
decodedate(qry.fieldbyname('dbdate').AsDateTime,l_n,l_y,l_r);
result:=encodedate(l_n,l_y,l_r);
end;

procedure Txt_bjtpwh_f.f_ShowJpg;
var JpgField:TBlobField;
begin
JpgField:=(q_sel.FieldByName('x_tx') as TBlobField);
u_jpg2bmp(image1.picture.bitmap,jpgfield);
image1.Hint:=q_sel.fieldbyname('x_xh').asstring;
end;

procedure u_jpg2bmp(ABmp:tbitmap;AField:TBlobField);
var jpg:tjpegimage; str:string;
begin
str:='.\bjt.jpg';
AField.SaveToFile(str);
try
jpg:=tjpegimage.Create;
try
jpg.LoadFromFile(Str);
Abmp.Assign(jpg);
finally jpg.Free; end;
finally DeleteFile(pchar(str)); end;
end;