第一步:通過$_FILES獲取文件信息。

“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)建站的服務宗旨!把網(wǎng)站當作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設中就是為了建設一個不僅審美在線,而且實用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設計、網(wǎng)站優(yōu)化、網(wǎng)絡推廣、探索永無止境。
第二步:指定新文件名稱以及路徑,并賦值給一個變量。
第三步:通過move_uploaded_file上傳文件。
第四步:上傳成功后,將數(shù)值存入數(shù)據(jù)庫服務器目錄即可。
代碼如下
1.conn.php
??
$host="localhost";?//數(shù)據(jù)庫服務器名稱?
$user="root";?//用戶名?
$pwd="1721";?//密碼?
$conn=mysql_connect($host,$user,$pwd);?
mysql_query("SET?
character_set_connection=gb2312,?
character_set_results=gb2312,?
character_set_client=binary",$conn);?
if?($conn==FALSE)?
{?
echo?"center服務器連接失敗!br請刷新后重試。/center";?
return?true;?
}?
$databasename="database";//數(shù)據(jù)庫名稱?
do?
{?
$con=mysql_select_db($databasename,$conn);?
}while(!$con);?
if?($con==FALSE)?
{?
echo?"center打開數(shù)據(jù)庫失敗!br請刷新后重試。/center";?
return?true;?
}?
?
2.upload.php
?php?
if?($_GET['action']?==?"save"){?
include_once('conn.php');?
include_once('uploadclass.php');?
$title=$_POST['title'];?
$pic=$uploadfile;?
if($title?==?"")?
echo"Scriptwindow.alert('對不起!你輸入的信息不完整!');history.back()/Script";?
$sql="insert?into?upload(title,pic)?values('$title','$pic')";?
$result=mysql_query($sql,$conn);?
//echo"Scriptwindow.alert('信息添加成功');location.href='upload.php'/Script";?
}?
??
html?
head?
title文件上傳實例/title?
/head?
body?
form?method="post"?action="?action=save"?enctype="multipart/form-data"?
table?border=0?cellspacing=0?cellpadding=0?align=center?width="100%"?
tr?
td?width=55?height=20?align="center"?/TD?
td?height="16"?
table?width="48%"?height="93"?border="0"?cellpadding="0"?cellspacing="0"?
tr?
td標題:/td?
tdinput?name="title"?type="text"?id="title"/td?
/tr?
tr?
td文件:?/td?
tdlabel?
input?name="file"?type="file"?value="瀏覽"??
input?type="hidden"?name="MAX_FILE_SIZE"?value="2000000"?
/label/td?
/tr?
tr?
td?/td?
tdinput?type="submit"?value="上?傳"?name="upload"/td?
/tr?
/table/td?
/tr?
/table?
/form?
/body?
/html
3.uploadclass.php
?php?
$uploaddir?=?"upfiles/";//設置文件保存目錄?注意包含/?
$type=array("jpg","gif","bmp","jpeg","png");//設置允許上傳文件的類型?
$patch="upload/";//程序所在路徑?
//獲取文件后綴名函數(shù)?
function?fileext($filename)?
{?
return?substr(strrchr($filename,?'.'),?1);?
}?
//生成隨機文件名函數(shù)?
function?random($length)?
{?
$hash?=?'CR-';?
$chars?=?'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';?
$max?=?strlen($chars)?-?1;?
mt_srand((double)microtime()?*?1000000);?
for($i?=?0;?$i??$length;?$i++)?
{?
$hash?.=?$chars[mt_rand(0,?$max)];?
}?
return?$hash;?
}?
$a=strtolower(fileext($_FILES['file']['name']));?
//判斷文件類型?
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))?
{?
$text=implode(",",$type);?
echo?"您只能上傳以下類型文件:?",$text,"br";?
}?
//生成目標文件的文件名?
else{?
$filename=explode(".",$_FILES['file']['name']);?
do?
{?
$filename[0]=random(10);?//設置隨機數(shù)長度?
$name=implode(".",$filename);?
//$name1=$name.".Mcncc";?
$uploadfile=$uploaddir.$name;?
}?
while(file_exists($uploadfile));?
if?(move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))?
{?
if(is_uploaded_file($_FILES['file']['tmp_name']))?
{?
echo?"上傳失敗!";?
}?
else?
{//輸出圖片預覽?
echo?"center您的文件已經(jīng)上傳完畢?上傳圖片預覽:?/centerbrcenterimg?src='$uploadfile'/center";?
echo?"brcentera?href='upload.htm'繼續(xù)上傳/a/center";?
}?
}?
}?
?
php實現(xiàn)上傳圖片保存到數(shù)據(jù)庫的方法。具體分析如下:
php 上傳圖片,一般都使用move_uploaded_file方法保存在服務器上。但如果一個網(wǎng)站有多臺服務器,就需要把圖片發(fā)布到所有的服務器上才能正常使用(使用圖片服務器的除外)
如果把圖片數(shù)據(jù)保存到數(shù)據(jù)庫中,多臺服務器間可以實現(xiàn)文件共享,節(jié)省空間。
首先圖片文件是二進制數(shù)據(jù),所以需要把二進制數(shù)據(jù)保存在mysql數(shù)據(jù)庫。
mysql數(shù)據(jù)庫提供了BLOB類型用于存儲大量數(shù)據(jù),BLOB是一個二進制對象,能容納不同大小的數(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??
}??
?
程序運行截圖和數(shù)據(jù)庫截圖:
把圖片src保存到服務器數(shù)據(jù)庫
走數(shù)據(jù)庫直接訪問表查詢存放images字段就好了
后臺:
//假設$res是你已經(jīng)從數(shù)據(jù)庫取出的值(已經(jīng)遍歷過了)
$dir = '根目錄/public/images/';
echo 'img src='.$dir.$res.'';
在前臺這樣顯示img src='根目錄/public/images/***.png'
?php
//將圖片存進數(shù)據(jù)庫再讀出,注意存儲圖片的字段類型必須為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'];
?
你在上傳圖片的時候,把圖片地址存到數(shù)據(jù)庫內(nèi),直接查詢輸出就好了
如果不是通過代碼上傳的,www文件夾是根目錄文件夾,圖片地址為:http://你的域名/img/name.jpg
文章名稱:php服務器圖片數(shù)據(jù) PHP圖片分享網(wǎng)站管理系統(tǒng)
地址分享:http://chinadenli.net/article40/dodpiho.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制開發(fā)、響應式網(wǎng)站、微信公眾號、網(wǎng)站維護、靜態(tài)網(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)