php將圖片以二進位儲存到mysql資料庫並顯示

來源:互聯網
上載者:User

標籤:

一.儲存圖片的資料表結構:

---- 表的結構 `image`--CREATE TABLE IF NOT EXISTS `image` (  `id` int(3) NOT NULL AUTO_INCREMENT,  `name` varchar(100) CHARACTER SET utf8 NOT NULL,  `pic` blob NOT NULL,  `type` varchar(50) CHARACTER SET utf8 NOT NULL,  `date` datetime NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;---- 轉存表中的資料 `image`--

  二.php將圖片以二進位儲存到mysql資料庫中:

(1)串連資料庫檔案:conn.php:
<?php$conn = mysql_connect(‘localhost‘, ‘root‘, ‘‘);mysql_select_db(‘study‘, $conn);mysql_query("SET NAMES UTF-8");?>

  (2)圖片上傳並以二進位儲存到資料庫檔案:upload.php

<?phpinclude(‘./conn.php‘);if ($_POST[‘submit‘]) {    if ($_FILES[‘image‘][‘size‘]) {        $names = $_FILES[‘image‘][‘name‘];        $arr   = explode(‘.‘, $names);        $name  = $arr[0]; //圖片名稱        $date  = date(‘Y-m-d H:i:s‘); //上傳日期        $fp    = fopen($_FILES[‘image‘][‘tmp_name‘], ‘rb‘);        $type  = $_FILES[‘image‘][‘type‘];        if (!$fp) {            showInfo(‘讀取圖片失敗!‘);        } else {            $image = addslashes(fread($fp, filesize($_FILES[‘image‘][‘tmp_name‘])));            if ($image) {                $q      = "insert into image (name, pic, type, date) values (‘$name‘,‘$image‘,‘$type‘,‘$date‘)";                $result = mysql_query($q);                if ($result) {                    showInfo(‘上傳成功!‘);                } else {                    showInfo(‘上傳失敗!‘);                }                            } else {                showInfo(‘請選擇要上傳的檔案!‘);            }        }            } else {        showInfo(‘請選擇要上傳的檔案!‘);    }}function showInfo($info){    echo "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";    echo "<meta http-equiv=‘refresh‘ content=‘1;url=index.php‘>";    echo "</head>";    echo "<body>" . $info . "……</body>";    echo "</html>";}?>

  三.從資料庫中讀取以二進位儲存的圖片並顯示:image.php

<?phpinclude(‘./conn.php‘);$id     = $_GET[‘id‘];$sql    = "select * from image where id=‘$id‘";$result = mysql_query($sql, $conn);if (!$result)    die("讀取圖片失敗!");$num = mysql_num_rows($result);if ($num < 1)    die("暫無圖片");$data = mysql_result($result, 0, ‘pic‘);$type = mysql_result($result, 0, ‘type‘);mysql_close($id);Header("Content-type: $type");echo $data;?>

  四.上傳並顯示圖片的頁面:index.php

<?phpinclude(‘./conn.php‘);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US"><!-- * Created on 2012-10-20 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates--> <head> <meta http-equive="Content-Type" content=text/html charset=utf-8>  <title> </title> </head> <body> <form method=‘post‘ action=‘./upload.php‘ enctype="multipart/form-data"><input type="file" name="image" /><input type="submit" name="submit" value="上傳" /></form><!-----------顯示圖片---------------------><table><?php$ret = mysql_query(‘select * from image order by id desc‘);if ($ret) {    while ($row = mysql_fetch_array($ret)) {?><tr><td style=‘width:170px;‘><img src="image.php?id=<?php        echo $row[id];?>"  width="170" height="150" border="0"><div style=‘text-align:center;‘><?php        echo $row[‘name‘];?></div><?php        echo $row[‘date‘];?></td></tr><?php    }}?></table><!-----------/顯示圖片---------------------> </body></html>

  

php將圖片以二進位儲存到mysql資料庫並顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.