本文目錄一覽:
- 1、php如何上傳圖片到資料庫
- 2、怎樣用php實現上傳圖片到資料庫
- 3、php中如何調用資料庫中的圖片並且顯示到頁面
- 4、php中如何從資料庫中讀取圖片?
- 5、php圖片保存到資料庫
- 6、php中如何將圖片儲存在資料庫里
php如何上傳圖片到資料庫
把圖片保存到伺服器,拼接圖片地址
保存圖片地址到資料庫
讀取圖片地址就能訪問到圖片了。
怎樣用php實現上傳圖片到資料庫
php實現上傳圖片保存到資料庫的方法。具體分析如下:
php 上傳圖片,一般都使用move_uploaded_file方法保存在伺服器上。但如果一個網站有多台伺服器,就需要把圖片發布到所有的伺服器上才能正常使用(使用圖片伺服器的除外)
如果把圖片數據保存到資料庫中,多台伺服器間可以實現文件共享,節省空間。
首先圖片文件是二進位數據,所以需要把二進位數據保存在mysql資料庫。
mysql資料庫提供了BLOB類型用於存儲大量數據,BLOB是一個二進位對象,能容納不同大小的數據。
BLOB類型有以下四種,除存儲的最大信息量不同外,其他都是一樣的。可根據需要使用不同的類型。
TinyBlob 最大 255B
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G
數據表photo,用於保存圖片數據,結構如下:
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
// 連接資料庫
$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
}
?
程序運行截圖和資料庫截圖:
php中如何調用資料庫中的圖片並且顯示到頁面
php是採用二進位形式存儲圖片及讀取顯示的,首先通過代碼創建數據表,然後上傳圖片伺服器再通過瀏覽器顯示,具體編程代碼舉例:
1、首先需要創建數據表,具體代碼如下圖所示。
2、然後寫上傳圖片到伺服器的頁面 upimage.html用來將圖片上傳資料庫,如下圖所示代碼。
3、處理圖片上傳的php upimage.php文件,如下圖所示圖片已儲存到資料庫。
4、顯示圖片的php getimage.php文件,為了看一下效果提前把ID寫入代碼。
5、預覽網站從資料庫中提取了圖片,並顯示到頁面上。
php中如何從資料庫中讀取圖片?
?php
//將圖片存進資料庫再讀出,注意存儲圖片的欄位類型必須為blob
$user=』root』;
$password=』root』;
$db=』test』;
$connect=mysql_connect(『localhost』,$user,$password);
mysql_set_charset(『utf8′,$connect);
mysql_select_db($db);
$photo = 「0x」.bin2hex(file_get_contents(「./test.jpg」));
$sql=」INSERT INTO `test`.`test` (`photo`) VALUES ($photo);」;//$photo不需要用引號,切記
mysql_query($sql);
//$result=mysql_query(「SELECT *
//FROM `test`
//LIMIT 0 , 30〃);
//$img=mysql_fetch_array($result);
//echo $img[‘photo’];
?
php圖片保存到資料庫
1.圖片轉換 將上傳的圖片讀取到一個字元串中,再用base64對數據進行編碼 $img =base64_encode(file_get_contents($_FILES[‘file_head’][‘tmp…
2.顯示圖片 imgsrc=”{$base64String}” 這樣就能把圖片顯示出來了
php中如何將圖片儲存在資料庫里
兩種方法:
一:將圖片上傳至指定目錄,在資料庫中保存文件名和文件路徑。
二:將圖片文件讀入字元串,將字元串保存到資料庫,不推薦(沒那麼長的欄位長度支持)。
原創文章,作者:GXISH,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/316866.html