图片上传到数据库
图片是可以存储到数据库中的,只是把它转化成二进制数据保存进去。但是这样的方式,将会使得数据库异常庞大。占用数据库资源。所以并不是主流的存储方式。
通常我们存储图片进入数据库的做法是,保存一个地址给数据库,而图片是采用别的方法存储到服务器磁盘中的。
比如,使用FTP方式将图片保存到服务器D:\PIC文件夹。在服务器数据库中只要记录D:\PIC\1.JPG。那么读取图片的时候数据库中读取图片文件名或地址,那么就可以在FTP中取得。
‘贰’ 如何将图片储存在Mysql数据库里
解决方法一般有两种:
1、将图片保存的路径存储到数据库;
2、将图片以二进制数据流的形式直接写入数据库字段中。
以下为具体方法:
一、保存图片的上传路径到数据库:
string
uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname =
this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string
dataName =
DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string
fileName = fileFullname.Substring(fileFullname.LastIndexOf("\") +
1);
//获取图片扩展名
string type =
fileFullname.Substring(fileFullname.LastIndexOf(".") +
1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg"
|| type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type ==
"GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload")
+ "\" + dataName + "." +
type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath
= "~/upload/" + dataName + "." +
type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using
System.Drawing;
using System.IO;
using
System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string
strPath = this.FileUpload1.PostedFile.FileName.ToString
();
//读取图片
FileStream fs = new System.IO.FileStream(strPath,
FileMode.Open, FileAccess.Read);
BinaryReader br = new
BinaryReader(fs);
byte[] photo =
br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection
myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User
ID=sa;Password=123");
string strComm = " INSERT INTO
stuInfo(stuid,stuimage) VALUES(107,@photoBinary
)";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm,
myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,
photo.Length);
myComm.Parameters["@photoBinary"].Value =
photo;
myConn.Open();
if (myComm.ExecuteNonQuery() >
0)
{
this.Label1.Text =
"ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand
command = new
SqlCommand("select stuimage from stuInfo where stuid=107",
mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar
();
//指定从数据库读取出来的图片的保存路径及名字
string strPath =
"~/Upload/zhangsan.JPG";
string strPhotoPath =
Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new
BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl
= strPath;
采用这两种方式可以根据实际需求灵活选择。
‘叁’ 图片如何存入数据库
通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
string uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname = this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//获取图片扩展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath = "~/upload/" + dataName + "." + type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//读取图片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定从数据库读取出来的图片的保存路径及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl = strPath;
采用俩种方式可以根据实际需求灵活选择。
‘肆’ 如何将图片存到数据库中
一般图片的处理都是上传到服务器然后将图片的地址名称依次保存在数据库中,取出时按照地址取出就可以。直接用网上的图片地址有的是可以的访问,有的因为图片加锁,保留网上那个图片地址最终是无法找到图片的。
‘伍’ 怎么把图片放到数据库当中啊
一般不把图片放到数据库,而是将图片的上传到某个文件夹,然后将图片的路径保存到数据库里
‘陆’ php如何上传图片到数据库
把图片保存到服务器,拼接图片地址
保存图片地址到数据库
读取图片地址就能访问到图片了。
‘柒’ 如何将上传图片和文字写进数据库
图片放在本地的某个目录中如“c:\image”,然后再把这个目录+图片名存入数据库"c:\image\image1.jpg"。读出时只要使用<img
href="c:\image\image1.jpg">就OK了。除此之外还可以把上传的图片作为字节流读入数据库,读出时使用cgi或Servlet程序把读出的流以图片显示出来。
‘捌’ 如何将图片存到数据库
通常对用户上传的图片需要保存到数据库中。解决方法一般有两种:一种是将图片保存的路径存储到数据库;另一种是将图片以二进制数据流的形式直接写入数据库字段中。以下为具体方法:
一、保存图片的上传路径到数据库:
string uppath="";//用于保存图片上传路径
//获取上传图片的文件名
string fileFullname = this.FileUpload1.FileName;
//获取图片上传的时间,以时间作为图片的名字可以防止图片重名
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
//获取图片的文件名(不含扩展名)
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
//获取图片扩展名
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
//判断是否为要求的格式
if (type == "bmp" || type == "jpg" || type == "jpeg" || type == "gif" || type == "JPG" || type == "JPEG" || type == "BMP" || type == "GIF")
{
//将图片上传到指定路径的文件夹
this.FileUpload1.SaveAs(Server.MapPath("~/upload") + "\\" + dataName + "." + type);
//将路径保存到变量,将该变量的值保存到数据库相应字段即可
uppath = "~/upload/" + dataName + "." + type;
}
二、将图片以二进制数据流直接保存到数据库:
引用如下命名空间:
using System.Drawing;
using System.IO;
using System.Data.SqlClient;
设计数据库时,表中相应的字段类型为iamge
保存:
//图片路径
string strPath = this.FileUpload1.PostedFile.FileName.ToString ();
//读取图片
FileStream fs = new System.IO.FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=.;Initial Catalog=stumanage;User ID=sa;Password=123");
string strComm = " INSERT INTO stuInfo(stuid,stuimage) VALUES(107,@photoBinary )";//操作数据库语句根据需要修改
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary, photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
if (myComm.ExecuteNonQuery() > 0)
{
this.Label1.Text = "ok";
}
myConn.Close();
读取:
...连接数据库字符串省略
mycon.Open();
SqlCommand command = new
SqlCommand("select stuimage from stuInfo where stuid=107", mycon);//查询语句根据需要修改
byte[] image = (byte[])command.ExecuteScalar ();
//指定从数据库读取出来的图片的保存路径及名字
string strPath = "~/Upload/zhangsan.JPG";
string strPhotoPath = Server.MapPath(strPath);
//按上面的路径与名字保存图片文件
BinaryWriter bw = new BinaryWriter(File.Open(strPhotoPath,FileMode.OpenOrCreate));
bw.Write(image);
bw.Close();
//显示图片
this.Image1.ImageUrl = strPath;
采用俩种方式可以根据实际需求灵活选择。
‘玖’ 怎样用php实现上传图片到数据库
php实现上传图片保存到数据库的方法。具体分析如下:
php 上传图片,一般都使用move_uploaded_file方法保存在服务器上。但如果一个网站有多台服务器,就需要把图片发布到所有的服务器上才能正常使用(使用图片服务器的除外)
如果把图片数据保存到数据库中,多台服务器间可以实现文件共享,节省空间。
首先图片文件是二进制数据,所以需要把二进制数据保存在mysql数据库。
mysql数据库提供了BLOB类型用于存储大量数据,BLOB是一个二进制对象,能容纳不同大小的数据。
BLOB类型有以下四种,除存储的最大信息量不同外,其他都是一样的。可根据需要使用不同的类型。
TinyBlob 最大 255B
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
数据表photo,用于保存图片数据,结构如下:
CREATETABLE`photo`(
`id`int(10)unsignedNOTNULLauto_increment,
`type`varchar(100)NOTNULL,
`binarydata`mediumblobNOTNULL,
PRIMARYKEY(`id`)
)ENGINE=MyISAMDEFAULTCHARSET=latin1AUTO_INCREMENT=1;
upload_image_todb.php代码如下:
<?php
//连接数据库
$conn=@mysql_connect("localhost","root","")ordie(mysql_error());
@mysql_select_db('demo',$conn)ordie(mysql_error());//判断action
$action=isset($_REQUEST['action'])?$_REQUEST['action']:'';
//上传图片
if($action=='add'){
$image=mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name']));
$type=$_FILES['photo']['type'];
$sqlstr="insertintophoto(type,binarydata)values('".$type."','".$image."')";
@mysql_query($sqlstr)ordie(mysql_error());
header('location:upload_image_todb.php');
exit();
//显示图片
}elseif($action=='show'){
$id=isset($_GET['id'])?intval($_GET['id']):0;
$sqlstr="select*fromphotowhereid=$id";
$query=mysql_query($sqlstr)ordie(mysql_error());
$thread=mysql_fetch_assoc($query);
if($thread){
header('content-type:'.$thread['type']);
echo$thread['binarydata'];
exit();
}
}else{
//显示图片列表及上传表单
?>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8">
<title>uploadimagetodbdemo</title>
</head>
<body>
<formname="form1"method="post"action="upload_image_todb.php"enctype="multipart/form-data">
<p>图片:<inputtype="file"name="photo"></p>
<p><inputtype="hidden"name="action"value="add"><inputtype="submit"name="b1"value="提交"></p>
</form>
<?php
$sqlstr="select*fromphotoorderbyiddesc";
$query=mysql_query($sqlstr)ordie(mysql_error());
$result=array();
while($thread=mysql_fetch_assoc($query)){
$result[]=$thread;
}
foreach($resultas$val){
echo'<p><img
src="upload_image_todb.php?action=show&id='.$val['id'].'&t='.time().'"
width="150"></p>';
}
?>
</body>
</html>
<?php
}
?>
程序运行截图和数据库截图: