php存圖片到數(shù)據(jù)庫 php圖片上傳到數(shù)據(jù)庫

怎樣把圖片插入到數(shù)據(jù)庫中 php

保存圖片到數(shù)據(jù)庫做什么?保存到本地使用起來也方便,真要保存通過base64字符串保存。

成都創(chuàng)新互聯(lián)主營蒼溪網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),蒼溪h5成都微信小程序搭建,蒼溪網(wǎng)站營銷推廣歡迎蒼溪等地區(qū)企業(yè)咨詢

?php

header('Content-type:text/html;charset=utf-8');

//讀取圖片文件,轉(zhuǎn)換成base64編碼格式

$image_file?=?'./image123.jpg';

$image_info?=?getimagesize($image_file);

$base64_image_content?=?"data:{$image_info['mime']};base64,"?.?chunk_split(base64_encode(file_get_contents($image_file)));

//?$base64_image_content?輸入到數(shù)據(jù)庫

//保存base64字符串為圖片

//匹配出圖片的格式

if?(preg_match('/^(data:\s*image\/(\w+);base64,)/',?$base64_image_content,?$result)){

$type?=?$result[2];

$new_file?=?"./test.{$type}";

if?(file_put_contents($new_file,?base64_decode(str_replace($result[1],?'',?$base64_image_content)))){

echo?'新文件保存成功:',?$new_file;

}

}

?

img?src="?php?echo?$base64_image_content;?"?/

php圖片保存到數(shù)據(jù)庫

1.圖片轉(zhuǎn)換 將上傳的圖片讀取到一個(gè)字符串中,再用base64對數(shù)據(jù)進(jìn)行編碼 $img =base64_encode(file_get_contents($_FILES['file_head']['tmp...

2.顯示圖片 imgsrc="{$base64String}" 這樣就能把圖片顯示出來了

PHP圖片上傳到數(shù)據(jù)庫

1首先最好不要把圖片存數(shù)據(jù)表。除非是做為資料保存。有些教材與網(wǎng)上的代碼的處理方式太老了,不要再模仿。當(dāng)然你的代碼中沒有看出來是用什么方式存儲圖片的。

2如果你是想把圖片存到數(shù)據(jù)表中,你的$file實(shí)際上只是文件名。應(yīng)該讀圖片的流數(shù)據(jù)寫到表中。

3如果你僅是存文件名到數(shù)據(jù)表,圖片在指定文件夾中存放,則應(yīng)該是出在路徑上。

怎樣用php實(shí)現(xiàn)上傳圖片到數(shù)據(jù)庫

php實(shí)現(xiàn)上傳圖片保存到數(shù)據(jù)庫的方法。具體分析如下:

php 上傳圖片,一般都使用move_uploaded_file方法保存在服務(wù)器上。但如果一個(gè)網(wǎng)站有多臺服務(wù)器,就需要把圖片發(fā)布到所有的服務(wù)器上才能正常使用(使用圖片服務(wù)器的除外)

如果把圖片數(shù)據(jù)保存到數(shù)據(jù)庫中,多臺服務(wù)器間可以實(shí)現(xiàn)文件共享,節(jié)省空間。

首先圖片文件是二進(jìn)制數(shù)據(jù),所以需要把二進(jìn)制數(shù)據(jù)保存在mysql數(shù)據(jù)庫。

mysql數(shù)據(jù)庫提供了BLOB類型用于存儲大量數(shù)據(jù),BLOB是一個(gè)二進(jìn)制對象,能容納不同大小的數(shù)據(jù)。

BLOB類型有以下四種,除存儲的最大信息量不同外,其他都是一樣的??筛鶕?jù)需要使用不同的類型。

TinyBlob?????? 最大 255B

Blob????????????? 最大 65K

MediumBlob? 最大 16M

LongBlob????? 最大 4G

數(shù)據(jù)表photo,用于保存圖片數(shù)據(jù),結(jié)構(gòu)如下:

CREATE?TABLE?`photo`?(??

`id`?int(10)?unsigned?NOT?NULL?auto_increment,??

`type`?varchar(100)?NOT?NULL,??

`binarydata`?mediumblob?NOT?NULL,??

PRIMARY?KEY??(`id`)??

)?ENGINE=MyISAM?DEFAULT?CHARSET=latin1?AUTO_INCREMENT=1?;

upload_image_todb.php代碼如下:

?php??

//?連接數(shù)據(jù)庫??

$conn=@mysql_connect("localhost","root","")??or?die(mysql_error());??

@mysql_select_db('demo',$conn)?or?die(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?=?"insert?into?photo(type,binarydata)?values('".$type."','".$image."')";??

@mysql_query($sqlstr)?or?die(mysql_error());??

header('location:upload_image_todb.php');??

exit();??

//?顯示圖片??

}elseif($action=='show'){??

$id?=?isset($_GET['id'])??intval($_GET['id'])?:?0;??

$sqlstr?=?"select?*?from?photo?where?id=$id";??

$query?=?mysql_query($sqlstr)?or?die(mysql_error());??

$thread?=?mysql_fetch_assoc($query);??

if($thread){??

header('content-type:'.$thread['type']);??

echo?$thread['binarydata'];??

exit();??

}??

}else{??

//?顯示圖片列表及上傳表單??

???

!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?""??

html??

head??

meta?http-equiv="content-type"?content="text/html;?charset=utf-8"??

title?upload?image?to?db?demo?/title??

/head??

body??

form?name="form1"?method="post"?action="upload_image_todb.php"?enctype="multipart/form-data"??

p圖片:input?type="file"?name="photo"/p??

pinput?type="hidden"?name="action"?value="add"input?type="submit"?name="b1"?value="提交"/p??

/form??

?php??

$sqlstr?=?"select?*?from?photo?order?by?id?desc";??

$query?=?mysql_query($sqlstr)?or?die(mysql_error());??

$result?=?array();??

while($thread=mysql_fetch_assoc($query)){??

$result[]?=?$thread;??

}??

foreach($result?as?$val){??

echo?'pimg?

src="upload_image_todb.php?action=showid='.$val['id'].'t='.time().'"

width="150"/p';??

}??

???

/body??

/html??

?php??

}??

?

程序運(yùn)行截圖和數(shù)據(jù)庫截圖:

分享文章:php存圖片到數(shù)據(jù)庫 php圖片上傳到數(shù)據(jù)庫
URL標(biāo)題:http://muchs.cn/article20/docosco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、Google定制網(wǎng)站、建站公司面包屑導(dǎo)航、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司