php從資料庫中顯示圖片
php是採用二進制形式存儲圖片及讀取顯示的,首先通過代碼創建數據表,然後上傳圖片伺服器再通過瀏覽器顯示,具體編程代碼舉例:
1、首先需要創建數據表,具體代碼如下圖所示。

『貳』 php中如何讓圖片顯示出來,現在可以從資料庫中讀出圖片路徑
$conn=mysql_connect("localhost","root","");//ip用戶名密碼
mysql_select_db("sitecms",$conn);//資料庫名
mysql_query("setnames'utf8'");//utf-8格式
$sql="selecturlfromlinkwherelid=3";
$result=mysql_query($sql,$conn);
while($row=mysql_fetch_array($result)){
echo"<imgsrc='".$row['url']."'/>";
}
『叄』 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怎麼把資料庫把圖片顯示出來的
跟調用其它欄位一樣啊
<img src="這里是你從資料庫調用出來的圖片的路徑" />
『伍』 php 從資料庫中顯示圖片
你吧圖片的路徑存入資料庫不就可以了嗎,
然後取出路徑就能顯示了
『陸』 PHP在資料庫中查詢並且顯示圖片
一般不向資料庫插入圖片 而是插入圖片的src 通過src找到圖片然後顯示。
<?php
session_start();
//array數組中放圖片的格式
$uptypes = array("image/jpg","image/jpeg","image/png","image/pjpeg","image/gif","image/bmp","image/x-png");
$files =$_FILES["uppic"];
if($files["size"]>2097152){ //圖片大小判斷
echo "上傳圖片不能大於2M";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=pic.php'>";
exit;
}
$ftype =$files["type"];
if(!in_array($ftype,$uptypes)){ //圖片格式判斷
echo "上傳的圖片文件格式不正確";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=pic.php'>";
}
$fname = $files["tmp_name"]; //在伺服器臨時存儲名稱
$image_info = getimagesize($fname);
$name = $files["name"];
$str_name = pathinfo($name); //以數組的形式返迴文件路勁的信息
$extname = strtolower($str_name["extension"]); //把字元串改為小寫 extensiorn擴展名
$upload_dir = "upload/"; //upload文件夾
$file_name = date("YmdHis").rand(1000,9999).".".$extname;
$str_file = $upload_dir.$file_name; //文件目錄
//存入資料庫
$con=mysql_connect("localhost","root","");
if(!$con){
die(("資料庫連接失敗").mysql_error());
}
mysql_select_db("mywork",$con);
$sql="update user set picpath='$str_file' where user_name='$username'"; //將圖片地址插入資料庫mywork
mysql_query($sql,$con);
mysql_close($con);
if(!file_exists($upload_dir)){
mkdir($upload_dir); //創建目錄 成功則返回true 失敗則返回flase
}
if(!move_uploaded_file($files["tmp_name"],$str_file)){ //將上傳的文件移動到新的目錄 要移動文件和文件新目錄 成功則返回true
echo "圖片上傳失敗";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=插入失敗後希望跳轉的頁面>";
}
else{
//echo "<img src=".$str_file.">";
echo "圖片上傳成功";
echo "<meta http-equiv='REFRESH' CONTENT='1;URL=插入成功希望挑戰的頁面>";
}
