php將圖片儲存入mysql資料庫失敗的解決方案_php技巧

來源:互聯網
上載者:User

本文執行個體分析了php將圖片儲存入mysql資料庫失敗的解決方案。分享給大家供大家參考。具體分析如下:

圖片儲存資料庫並不是一個明智的做法,我們多半是把圖片儲存到伺服器,然後把圖片地址儲存到資料庫,這樣我們每次只要讀出圖片地址就可以顯示了,但下面我還是來介紹一個圖片儲存到mysql資料庫的問題解決辦法,代碼如下:

複製代碼 代碼如下:
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好用.

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.