Home > php教程 > php手册 > body text

php图片保存入mysql数据库失败解决办法

WBOY
Release: 2016-05-25 16:37:57
Original
1017 people have browsed it

图片保存数据库并不是一个明智的做法,我们多半是把图片保存到服务器,然后把图片地址保存到数据库,这样我们每次只要读出图片地址就可以显示了,但下面我还是来介绍一个图片保存到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') ;");
Copy after login

报错:

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的一样大.
Copy after login

例,前台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>
Copy after login

后台处理upimage.php代码如下:

<?php 
 //向数据库中插入图片 
 $imgfile=$_FILES[&#39;imgfile&#39;]; 
 $submitbtn=$_POST[&#39;submitbtn&#39;]; 
 if($submitbtn==&#39;OK&#39; and is_array($imgfile)){ 
 $name=$imgfile[&#39;name&#39;];  //取得图片名称 
 $type=$imgfile[&#39;type&#39;]; //取得图片类型 
 $size=$imgfile[&#39;size&#39;];  //取得图片长度 
 $tmpfile=$imgfile[&#39;tmp_name&#39;];  //图片上传上来到临时文件的路径 
 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(&#39;".$name."&#39;,&#39;".$type."&#39;,0x".$imgdata.")")) 
   echo "<center>插入成功!<br><br><a href=&#39;disimage.php&#39;>显示图片</a></center>"; 
  else 
   echo "<center>插入失败!</center>"; 
  mysql_close(); 
 }else 
 echo "<center>请先选择图片!<br><br><a href=&#39;image.html&#39;>点此返回</a></center>"; 
} else 
 echo "<center>请先选择图片!<br><br><a href=&#39;image.html&#39;>点此返回</a></center>"; 
?>
Copy after login

显示图片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(); 
?>
Copy after login

结论:

PMA_sqlAddslashes好用 文件12.8k 和原来图片一样大

bin2hex 16进制 好用文件25K

base64_encode 好用,出来的文件需要base64_decode 17K

addslashes 不好用,继续报错,注明,在某些windows机器上addslashes好用.


教程链接:

随意转载~但请保留教程地址★

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!