圖片上傳到資料庫
圖片是可以存儲到資料庫中的,只是把它轉化成二進制數據保存進去。但是這樣的方式,將會使得資料庫異常龐大。佔用資料庫資源。所以並不是主流的存儲方式。
通常我們存儲圖片進入資料庫的做法是,保存一個地址給資料庫,而圖片是採用別的方法存儲到伺服器磁碟中的。
比如,使用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
}
?>
程序運行截圖和資料庫截圖: