php将图片保存入mysql数据库失败的解决方法,
本文实例分析了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,代码如下:
复制代码 代码如下:
后台处理upimage.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 "
显示图片
else
echo "
mysql_close();
}else
echo "
点此返回
} else
echo "
点此返回
?>
显示图片disimage.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程序设计有所帮助。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
