本文實(shí)例講述了PHP實(shí)現(xiàn)上傳圖片到數(shù)據(jù)庫并顯示輸出的方法。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的靖邊網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
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
"圖片已存儲(chǔ)到數(shù)據(jù)庫";
}
else
{
echo
"請求失敗,請重試";
注:圖片是以二進(jìn)制blob形式存進(jìn)數(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)以二進(jìn)制的形式存儲(chǔ)到數(shù)據(jù)庫了
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:php實(shí)現(xiàn)上傳圖片保存到數(shù)據(jù)庫的方法php上傳圖片存入數(shù)據(jù)庫示例分享php上傳圖片到指定位置路徑保存到數(shù)據(jù)庫的具體實(shí)現(xiàn)php中如何將圖片儲(chǔ)存在數(shù)據(jù)庫里php下將圖片以二進(jìn)制存入mysql數(shù)據(jù)庫中并顯示的實(shí)現(xiàn)代碼php
從數(shù)據(jù)庫提取二進(jìn)制圖片的處理代碼php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法php圖片的二進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法
這里我要跟你說明一下,如果你在一個(gè)公司的話,公司的數(shù)據(jù)庫 是絕對(duì)不會(huì)讓你直接在數(shù)據(jù)庫里面存儲(chǔ)圖片的,只會(huì)存儲(chǔ)一個(gè)服務(wù)器的路徑而已,所以你要先把圖片路徑輸出出來 數(shù)據(jù)庫圖片類型 varchar
php是采用二進(jìn)制形式存儲(chǔ)圖片及讀取顯示的,首先通過代碼創(chuàng)建數(shù)據(jù)表,然后上傳圖片服務(wù)器再通過瀏覽器顯示,具體編程代碼舉例:
1、首先需要?jiǎng)?chuàng)建數(shù)據(jù)表,具體代碼如下圖所示。
2、然后寫上傳圖片到服務(wù)器的頁面 upimage.html用來將圖片上傳數(shù)據(jù)庫,如下圖所示代碼。
3、處理圖片上傳的php upimage.php文件,如下圖所示圖片已儲(chǔ)存到數(shù)據(jù)庫。
4、顯示圖片的php getimage.php文件,為了看一下效果提前把ID寫入代碼。
5、預(yù)覽網(wǎng)站從數(shù)據(jù)庫中提取了圖片,并顯示到頁面上。
網(wǎng)站題目:php怎么輸出數(shù)據(jù)庫圖片 php輸出數(shù)據(jù)庫圖片代碼
鏈接URL:http://chinadenli.net/article8/dodsdop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站策劃、虛擬主機(jī)、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(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)