本文實例講述了PHP實現(xiàn)上傳圖片到數(shù)據(jù)庫并顯示輸出的方法。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供始興網(wǎng)站建設(shè)、始興做網(wǎng)站、始興網(wǎng)站設(shè)計、始興網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、始興企業(yè)網(wǎng)站模板建站服務(wù),十年始興做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
1.
創(chuàng)建數(shù)據(jù)表
CREATE
TABLE
ccs_image
(
id
int(4)
unsigned
NOT
NULL
auto_increment,
description
varchar(250)
default
NULL,
bin_data
longblob,
filename
varchar(50)
default
NULL,
filesize
varchar(50)
default
NULL,
filetype
varchar(50)
default
NULL,
PRIMARY
KEY
(id)
)engine=myisam
DEFAULT
charset=utf8
2.
用于上傳圖片到服務(wù)器的頁面
upimage.html
!doctype
html
html
lang="en"
head
meta
charset="UTF-8"
meta
name="viewport"
content="width=device-width,
user-scalable=no,
initial-scale=1.0,
maximum-scale=1.0,
minimum-scale=1.0"
meta
http-equiv="X-UA-Compatible"
content="ie=edge"
style
type="text/css"
*{margin:
1%}
/style
titleDocument/title
/head
body
form
method="post"
action="upimage.php"
enctype="multipart/form-data"
描述:
input
type="text"
name="form_description"
size="40"
input
type="hidden"
name="MAX_FILE_SIZE"
value="1000000"
br
上傳文件到數(shù)據(jù)庫:
input
type="file"
name="form_data"
size="40"br
input
type="submit"
name="submit"
value="submit"
/form
/body
/html
3.
處理圖片上傳的php
upimage.php
?php
if
(isset($_POST['submit']))
{
$form_description
=
$_POST['form_description'];
$form_data_name
=
$_FILES['form_data']['name'];
$form_data_size
=
$_FILES['form_data']['size'];
$form_data_type
=
$_FILES['form_data']['type'];
$form_data
=
$_FILES['form_data']['tmp_name'];
$dsn
=
'mysql:dbname=test;host=localhost';
$pdo
=
new
PDO($dsn,
'root',
'root');
$data
=
addslashes(fread(fopen($form_data,
"r"),
filesize($form_data)));
//echo
"mysqlPicture=".$data;
$result
=
$pdo-query("INSERT
INTO
ccs_image
(description,bin_data,filename,filesize,filetype)
VALUES
('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
if
($result)
{
echo
"圖片已存儲到數(shù)據(jù)庫";
}
else
{
echo
"請求失敗,請重試";
注:圖片是以二進制blob形式存進數(shù)據(jù)庫的,像這樣
4.
顯示圖片的php
getimage.php
?php
$id
=2;//
$_GET['id'];
為簡潔,直接將id寫上了,正常應(yīng)該是通過用戶填入的id獲取的
$dsn='mysql:dbname=test;host=localhost';
$pdo=new
PDO($dsn,'root','root');
$query
=
"select
bin_data,filetype
from
ccs_image
where
id=2";
$result
=
$pdo-query($query);
$result=$result-fetchAll(2);
//
var_dump($result);
$data
=
$result[0]['bin_data'];
$type
=
$result[0]['filetype'];
Header(
"Content-type:
$type");
echo
$data;
到瀏覽器查看已經(jīng)上傳的圖片,看是否可以顯示
是沒有問題的,證明圖片已經(jīng)以二進制的形式存儲到數(shù)據(jù)庫了
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:php實現(xiàn)上傳圖片保存到數(shù)據(jù)庫的方法php上傳圖片存入數(shù)據(jù)庫示例分享php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫的具體實現(xiàn)php中如何將圖片儲存在數(shù)據(jù)庫里php下將圖片以二進制存入mysql數(shù)據(jù)庫中并顯示的實現(xiàn)代碼php
從數(shù)據(jù)庫提取二進制圖片的處理代碼php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法php將圖片文件轉(zhuǎn)換成二進制輸出的方法php圖片的二進制轉(zhuǎn)換實現(xiàn)方法
復(fù)制代碼
代碼如下:
?php
/*
MYSQL
數(shù)據(jù)庫訪問封裝類
MYSQL
數(shù)據(jù)訪問方式,php4支持以mysql_開頭的過程訪問方式,php5開始支持以mysqli_開頭的過程和mysqli面向?qū)ο?/p>
訪問方式,本封裝類以mysql_封裝
數(shù)據(jù)訪問的一般流程:
1,連接數(shù)據(jù)庫
mysql_connect
or
mysql_pconnect
2,選擇數(shù)據(jù)庫
mysql_select_db
3,執(zhí)行SQL查詢
mysql_query
4,處理返回的數(shù)據(jù)
mysql_fetch_array
mysql_num_rows
mysql_fetch_assoc
mysql_fetch_row
etc
*/
class
db_mysql
{
var
$querynum
=
;
//當(dāng)前頁面進程查詢數(shù)據(jù)庫的次數(shù)
var
$dblink
;
//數(shù)據(jù)庫連接資源
//鏈接數(shù)據(jù)庫
function
connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0
,
$halt=true)
{
$func
=
empty($pconnect)
?
'mysql_connect'
:
'mysql_pconnect'
;
$this-dblink
=
@$func($dbhost,$dbuser,$dbpw)
;
if
($halt
!$this-dblink)
{
$this-halt("無法鏈接數(shù)據(jù)庫!");
}
//設(shè)置查詢字符集
mysql_query("SET
character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this-dblink)
;
//選擇數(shù)據(jù)庫
$dbname
@mysql_select_db($dbname,$this-dblink)
;
}
//選擇數(shù)據(jù)庫
function
select_db($dbname)
{
return
mysql_select_db($dbname,$this-dblink);
}
//執(zhí)行SQL查詢
function
query($sql)
{
$this-querynum++
;
return
mysql_query($sql,$this-dblink)
;
}
//返回最近一次與連接句柄關(guān)聯(lián)的INSERT,UPDATE
或DELETE
查詢所影響的記錄行數(shù)
function
affected_rows()
{
return
mysql_affected_rows($this-dblink)
;
}
//取得結(jié)果集中行的數(shù)目,只對select查詢的結(jié)果集有效
function
num_rows($result)
{
return
mysql_num_rows($result)
;
}
//獲得單格的查詢結(jié)果
function
result($result,$row=0)
{
return
mysql_result($result,$row)
;
}
//取得上一步
INSERT
操作產(chǎn)生的
ID,只對表有AUTO_INCREMENT
ID的操作有效
function
insert_id()
{
return
($id
=
mysql_insert_id($this-dblink))
=
?
$id
:
$this-result($this-query("SELECT
last_insert_id()"),
0);
}
//從結(jié)果集提取當(dāng)前行,以數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_row($result)
{
return
mysql_fetch_row($result)
;
}
//從結(jié)果集提取當(dāng)前行,以字段名為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//從結(jié)果集提取當(dāng)前行,以字段名和數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//關(guān)閉鏈接
function
close()
{
return
mysql_close($this-dblink)
;
}
//輸出簡單的錯誤html提示信息并終止程序
function
halt($msg)
{
$message
=
"html\nhead\n"
;
$message
.=
"meta
content='text/html;charset=gb2312'\n"
;
$message
.=
"/head\n"
;
$message
.=
"body\n"
;
$message
.=
"數(shù)據(jù)庫出錯:".htmlspecialchars($msg)."\n"
;
$message
.=
"/body\n"
;
$message
.=
"/html"
;
echo
$message
;
exit
;
}
}
?
php封裝mysql類
復(fù)制代碼
代碼如下:
?php
class
Mysql
{
private
$host;
private
$user;
private
$pwd;
private
$dbName;
private
$charset;
private
$conn
=
null;
public
function
__construct()
{
$this-host
=
'localhost';
$this-user
=
'root';
$this-pwd
=
'root';
$this-dbName
=
'test';
$this-connect($this-host,$this-user,$this-pwd);
$this-switchDb($this-dbName);
$this-setChar($this-charset);
}
//負(fù)責(zé)鏈接
private
function
connect($h,$u,$p)
{
$conn
=
mysql_connect($h,$u,$p);
$this-conn
=
$conn;
}
//負(fù)責(zé)切換數(shù)據(jù)庫
public
function
switchDb($db)
{
$sql
=
'use'
.
$db;
$this-query($sql);
}
//負(fù)責(zé)設(shè)置字符集
public
function
setChar($char)
{
$sql
=
'set
names'
.
$char;
$this-query($sql);
}
//負(fù)責(zé)發(fā)送sql查詢
public
function
query($sql)
{
return
mysql_query($sql,$this-conn);
}
//負(fù)責(zé)獲取多行多列的select結(jié)果
public
function
getAll($sql)
{
$list
=
array();
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
while
($row
=
mysql_fetch_assoc($rs))
{
$list[]
=
$row;
}
return
$list;
}
public
function
getRow($sql)
{
$rs
=
$this-query($sql);
if(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
}
public
function
getOne($sql)
{
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
return
$row[0];
}
public
function
close()
{
mysql_close($this-conn);
}
}
echo
'pre';
$mysql
=
new
Mysql();
print_r($mysql);
$sql
=
"insert
into
stu
values
(4,'wangwu','99998')";
if($mysql-query($sql)){
echo
"query成功";
}else
{
echo
"失敗";
}
echo
"br
/";
$sql
=
"select
*
from
stu";
$arr
=
$mysql-getAll($sql);
print_r($arr);
?
把你的上傳部分寫在function.php或者繼承的父級控制器就行。
如:
/*文件上傳*/
function?_uploads($savePath='/Picture/'){
$upload?=?new?\Think\Upload();
$upload-maxSize?=?3*1024*1024;//?設(shè)置附件上傳大小
$upload-exts?=?array('jpg',?'gif',?'png',?'jpeg',?'bmp');//?設(shè)置附件上傳類型
$upload-autoSub?=?true;//自動子目錄保存文件
$upload-subName?=?array('date','Y-m-d');//子目錄創(chuàng)建方式
$upload-saveName?=?array('uniqid','');//上傳文件命名規(guī)則,唯一性
$upload-rootPath?=?'./Uploads/';?//?設(shè)置附件上傳根目錄
$upload-savePath?=?$savePath;?//?設(shè)置附件上傳(子)目錄
//?上傳文件
$info?=?$upload-upload();
return?$info;
}
//此處方法調(diào)用
public?function?upFile(){
$file?=?_uploads('/Picture/');//里面可填寫子目錄文件路徑,支持多文件上傳
if($file){
$img?=?M('img');
//...此處添加所需邏輯處理
foreach?($file?as?$v)?{
//...此處添加所需邏輯處理
//拼接圖片地址用于保存數(shù)據(jù)庫
$dataImg['url']?=?'/Uploads'?.?$v['savepath']?.?$v['savename'];//根據(jù)需求拼接路徑,也可在/Uploads前拼成根路徑加上項目名稱
$img-add($dataImg);
}
}
}
網(wǎng)頁題目:php數(shù)據(jù)庫封裝運行圖,php數(shù)據(jù)庫封裝類
標(biāo)題路徑:http://chinadenli.net/article12/hsjidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、標(biāo)簽優(yōu)化、App設(shè)計、網(wǎng)站收錄、動態(tài)網(wǎng)站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)