| 代碼如下 |
複製代碼 |
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) |
繼續報錯
各種搜尋,百度裡的結果都是addslashes,要不就是addslashes也沒有的。真是扯淡啊
base64_decode
$img = base64_encode($img);
插入成功。圖片檔案17.0k
出來進行base64_decode ,顯示正常
找到個16進位的辦法
$img = bin2hex($img);
有效,輸出不用解密 。存入資料庫很大 25K。比base64還坑爹呢。
再找。
後來,後來。發現phpmyadmin直接上傳的圖片檔案可以用檔案比base64的小。檔案12.8k
翻phpmyadmin 原始碼
common.lib.php檔案183有個神奇的函數
| 代碼如下 |
複製代碼 |
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); } 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 //向資料庫中插入圖片 $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()將位元據轉換成十六進位表示 fclose($file); $mysqli=mysql_connect("localhost","root","123456″); //串連資料庫函數 mysql_select_db("test"); //選擇資料庫 //插入出資料庫語句,圖片資料前要加上0x,用於表示16進位數 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(); ?> |
結論
PMA_sqlAddslashes好用 檔案12.8k 和原來圖片一樣大
bin2hex 16進位 好用檔案25K
base64_encode 好用,出來的檔案需要base64_decode 17K
addslashes 不好用,繼續報錯(註明:在某些windows機器上addslashes好用)