圖片保存數(shù)據(jù)庫并不是一個明智的做法,我們多半是把圖片保存到服務(wù)器,然后把圖片地址保存到數(shù)據(jù)庫,這樣我們每次只要讀出圖片地址就可以顯示了,但下面我還是來介紹一個圖片保存到mysql數(shù)據(jù)庫的問題解決辦法,代碼如下:
- require 'class/db.php';
- $fileName = "a1.jpg";
- $fp = fopen($fileName, "r");
- $img = fread($fp, filesize($fileName));
- fclose($fp);
- $db->execute("insert db2.testimg (`img`) values ('$img') ;");
報錯:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`?綬q?仳!????1丶>,Mo?'^WZ4in??T春??????U?楹/?' at line 1
代碼如下:
$img = fread($fp, filesize($fileName));$img = addslashes($img)
繼續(xù)報錯,各種搜索,百度里的結(jié)果都是addslashes,要不就是addslashes也沒有的,真是扯淡啊.
base64_decode
$img = base64_encode($img);
插入成功,圖片文件17.0k,出來進(jìn)行base64_decode,顯示正常,找到個16進(jìn)制的辦法:
$img = bin2hex($img);
有效,輸出不用解密,存入數(shù)據(jù)庫很大 25K,比base64還坑爹呢,再找,后來,后來,發(fā)現(xiàn)phpmyadmin直接上傳的圖片文件可以用文件比base64的小,文件12.8k.
翻phpmyadmin 源代碼,common.lib.php文件183有個神奇的函數(shù),代碼如下:
- function PMA_sqlAddslashes($a_string = '', $is_like = false, $crlf = false, $php_code = false)
- {
- if ($is_like) {
- $a_string = str_replace('/', '////', $a_string);
- } else {
- $a_string = str_replace('/', '//', $a_string);
- }
- if ($crlf) {
- $a_string = str_replace("n", 'n', $a_string);
- $a_string = str_replace("r", 'r', $a_string);
- $a_string = str_replace("t", 't', $a_string);
- }//開源代碼Vevb.com
- if ($php_code) {
- $a_string = str_replace(''', '/'', $a_string);
- } else {
- $a_string = str_replace(''', '''', $a_string);
- }
- return $a_string;
- } // end of the 'PMA_sqlAddslashes()' function$img = PMA_sqlAddslashes($img);
文件大小12.8K 和phpmyadmin的一樣大.
例,前臺image.html,代碼如下:
- <html>
- <head>
- <title>上傳圖片</title>
- </head>
- <body>
- <form method="post" action="upimage.php" enctype="multipart/form-data">
- <input type="hidden" value="204800" name="MAX_FILE_SIZE"/>
- File: <input type="file" name="imgfile" />
- <input type="submit" value="OK" name="submitbtn" style="width:100px;height:23px"/></center>
- </form>
- </body>
- </html>
后臺處理upimage.php代碼如下:
- <?php
- //向數(shù)據(jù)庫中插入圖片
- $imgfile=$_FILES['imgfile'];
- $submitbtn=$_POST['submitbtn'];
- if($submitbtn=='OK' and is_array($imgfile)){
- $name=$imgfile['name']; //取得圖片名稱
- $type=$imgfile['type']; //取得圖片類型
- $size=$imgfile['size']; //取得圖片長度
- $tmpfile=$imgfile['tmp_name']; //圖片上傳上來到臨時文件的路徑
- if($tmpfile and is_uploaded_file($tmpfile)){ //判斷上傳文件是否為空,文件是不是上傳的文件
- //讀取圖片流
- $file=fopen($tmpfile,"rb");
- $imgdata=bin2hex(fread($file,$size)); //bin2hex()將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成十六進(jìn)制表示
- fclose($file);
- $mysqli=mysql_connect("localhost","root","123456″); //連接數(shù)據(jù)庫函數(shù)
- mysql_select_db("test"); //選擇數(shù)據(jù)庫
- //插入出數(shù)據(jù)庫語句,圖片數(shù)據(jù)前要加上0x,用于表示16進(jìn)制數(shù)
- if(mysql_query("insert into images(name,type,image) values('".$name."','".$type."',0x".$imgdata.")"))
- echo "<center>插入成功!<br><br><a href='disimage.php'>顯示圖片</a></center>";
- else
- echo "<center>插入失敗!</center>";
- mysql_close();
- }else
- echo "<center>請先選擇圖片!<br><br><a href='image.html'>點此返回</a></center>";
- } else
- echo "<center>請先選擇圖片!<br><br><a href='image.html'>點此返回</a></center>";
- ?>
顯示圖片disimage.php,代碼如下:
- <?php
- mysql_connect("localhost","root","123456″);
- mysql_select_db("test");
- //顯示最新插入的那張圖片
- $result=mysql_query("select image from images where id=(select max(id) from images)");
- $row=mysql_fetch_object($result);
- header("Content-Type:image/pjpeg");
- echo $row->image;
- mysql_close();
- ?>
結(jié)論:
PMA_sqlAddslashes好用 文件12.8k 和原來圖片一樣大
bin2hex 16進(jìn)制 好用文件25K
base64_encode 好用,出來的文件需要base64_decode 17K
addslashes 不好用,繼續(xù)報錯,注明,在某些windows機(jī)器上addslashes好用.
新聞熱點
疑難解答