只能告訴你思路,思路一說估計你就會了,
創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、五指山網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為五指山等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
首先你要有數(shù)據(jù)表去存圖片,
其次你要回php的文件上傳,當(dāng)文件上傳成功后你把上傳的圖片存到你的服務(wù)器上面具體位置也是你自己定的,這樣你就可以把你上傳上來的圖片所放置的位置存到數(shù)據(jù)庫就好了。
當(dāng)你問到這個問題的時候,其實我是不想回答的
你應(yīng)該是個小白,因為 “文件地址”這個其實就是一個URL,就是一個字符串。把“
文件地址上傳到數(shù)據(jù)庫”,其實就是把一個字符串插入到相應(yīng)的表。如果你有基礎(chǔ),文件都上傳了獲取到路徑了,就等插入數(shù)據(jù)庫,根本不會來問這個問題。如果你沒基礎(chǔ),那不是我在這里三言兩語甚至長篇大論能教會你的。
我只能大概說一下思路:
1、首先,建表,數(shù)據(jù)庫新建一個表來存放這個文件地址
2、鏈接這個數(shù)據(jù)庫
3、把這個“文件地址”,插到數(shù)據(jù)庫里面
完了!
需要PHP基礎(chǔ)知識和數(shù)據(jù)庫基礎(chǔ)知識。
以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫。
mysql_connect() 函數(shù)打開MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個參數(shù)分別是服務(wù)器名,連接賬號,連接密碼。
連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接
本文實例講述了PHP實現(xiàn)上傳圖片到數(shù)據(jù)庫并顯示輸出的方法。分享給大家供大家參考,具體如下:
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
"請求失敗,請重試";
注:圖片是以二進(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)制的形式存儲到數(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下將圖片以二進(jìn)制存入mysql數(shù)據(jù)庫中并顯示的實現(xiàn)代碼php
從數(shù)據(jù)庫提取二進(jìn)制圖片的處理代碼php將圖片保存入mysql數(shù)據(jù)庫失敗的解決方法php將圖片文件轉(zhuǎn)換成二進(jìn)制輸出的方法php圖片的二進(jìn)制轉(zhuǎn)換實現(xiàn)方法
新聞標(biāo)題:php上傳到數(shù)據(jù)庫 php傳輸數(shù)據(jù)的方式
鏈接地址:http://muchs.cn/article12/dospdgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、ChatGPT、虛擬主機(jī)、小程序開發(fā)、網(wǎng)頁設(shè)計公司
聲明:本網(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)