国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

用PHP實現把圖象上傳到數據庫

2024-05-04 23:05:06
字體:
來源:轉載
供稿:網友
  有什么問題請與我聯系:http://www.survivalescaperooms.com [email protected]
 轉載請注明出處

  今天教給大家如何用php實現把圖象上傳到mysql數據庫中。 在這個教程中我們需要建立3個php文件:

  readdir.php - 把圖片放到數據庫的代碼
  image.php - 顯示實際圖片的代碼
  view.php - 顯示你如何調用數據庫中的圖片的代碼

1.創建一個數據庫

create table `images` (
`imgid` int not null auto_increment ,
`sixfourdata` longtext not null ,
primary key ( `imgid` )
);
readdir.php

具體的內容:
<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
?>
'我們需要打開一個目錄
"./"
'readdir.php 文件定位于這個目錄:
$path = "./";
$dir_handle = opendir($path) or die("unable to open directory $path");

  下面是比較難的部分,大家需要好好研究一下:把圖象分類,并且讀出正在使用的一些數據

fopen

'轉換

base64_encode

' 插入到表里

<?
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' or $filetyp == 'jpg') {
$handle = fopen($path . "/" . $file,'r');
$file_content = fread($handle,filesize($path . "/" . $file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = "insert into images set sixfourdata='$encoded'";
mysql_query($sql);
}
}
?>

  關閉設置的目錄,然后處理:

<?
closedir($dir_handle);
echo("complete");
mysql_close($dbcnx);
?>

  讀出圖片的代碼:image.php
  這段代碼比較難,我們要好好看看

<?
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");
?>
我們讀出圖片使用的代碼image.php?img=x:
<?
$img = $_request["img"];
?>
之后我們需要連接數據庫,然后讀出
<?
$result = mysql_query("select * from images where imgid=" . $img . "");
if (!$result) {
echo("<b>請求錯誤: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result)) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];
}
?>

<?
mysql_close($dbcnx);
echo base64_decode($encodeddata);
?>

  在這里我們要理解base64-encoded 圖象數據格式。

  "讓我們來看看具體的圖片吧!" view.php

image.php?img=1

image.php?img=357

<img src='image.php?img=1' border="0" alt="">

看看完整的一個例子吧!

readdir.php:
<?
###############################
# db connection
# change these values
###############################
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("base64imgdb");

$path = "./";
$dir_handle = opendir($path) or die("unable to open directory $path");
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' or $filetyp == 'jpg') {
$handle = fopen($file,'r');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = "insert into images set sixfourdata='$encoded'";
mysql_query($sql);
}
}

closedir($dir_handle);
echo("complete");
mysql_close($dbcnx);
?>
image.php:
<?
$dbcnx = mysql_connect("localhost", "username", "password");

mysql_select_db("base64imgdb");

$img = $_request["img"];

$result = mysql_query("select * from images where imgid=" . $img . "");

if (!$result) {

echo("<b>error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result) ) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];
}
mysql_close($dbcnx);
echo base64_decode($encodeddata);
?>
and view.php (i shouldnt need to post this..)
<html>
<body>
..
<img src='image.php?img=1' border="0" alt="">
..
</body>
</html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 航空| 洛隆县| 盐源县| 江都市| 公主岭市| 西吉县| 新沂市| 兖州市| 阜阳市| 田东县| 新绛县| 句容市| 安康市| 南澳县| 东乌珠穆沁旗| 海宁市| 西和县| 香港| 靖安县| 望都县| 锦屏县| 赤壁市| 西盟| 辽宁省| 万全县| 杨浦区| 电白县| 禄丰县| 昌邑市| 东明县| 海林市| 邢台县| 毕节市| 玉林市| 新安县| 山丹县| 石门县| 咸宁市| 上虞市| 澎湖县| 石河子市|