search
HomeBackend DevelopmentPHP Tutorial 修改上传图片?解决方案

修改上传图片?


  选择图片地址:
 
 
我可以上传图片,可修改就成了问题,因为图片是以二进制流blob的形式储存到数据库中,所以不知道怎么读到这个地址,就是在我修改了图片名字没修改地址时,存在数据库中的图片地址不变

------解决方案--------------------
图片转为二进制 

$img_file = 'xxx.jpg';
$fp = fopen($img_file, 'rb');
$content = fread($fp, filesize($img_file)); //二进制数据
fclose($fp);



查询 显示:
header("content-type:image/jpeg");
list($st1,$st2)=explode(" ",microtime());
$startTime=$st1+$st2;
//$picContent=file_get_contents("1.jpg");
$lnk=mysql_connect("localhost","root","123456");
mysql_select_db("test");
//$picContents=base64_encode($picContent);
//$re=mysql_query("insert into pic values(NULL,'$picContents')");
$re=mysql_query("select * from pic");
!$re && die("查询失败!~");
$rs=mysql_fetch_array($re);
mysql_close($lnk);
echo base64_decode($rs['pic']);
list($st1,$st2)=explode(" ",microtime());
$time2=$st1+$st2;
echo '执行时间'.($time2-$startTime);
?>


建议:将二进制数据先base64编码后,再加入数据库,不然一些特殊符号会导致插入不成功。
------解决方案--------------------
上传处理:

//由于上传过来的图片被保存在一个临时文件中,所以
//我们仅需要读取该文件就可以获取传过来的图片
$fp = fopen($_FILES["myFile"]["tmp_name"],"rb");
$buf = addslashes(fread($fp,$_FILES["myFile"]["size"]));
//创建一个PDO对象
$dbh = new PDO("mysql:host=localhost;port=
3306;dbname=myDatabase", "root", "verysecret");
//执行插入操作并将结果保存在一个变量中
$result = $dbh->query("INSERT INTO employees 
(firstName,lastName,EmpType,age,picture) VALUES 
('myFirst','myLast','myEmpType',50,'$buf')");
//获取影响的行数
if ($result->rowCount() >0) {
echo("数据已插入。");
} else {
echo("不能执行插入操作。");
}
//显式的关闭PDO连接
$dbh = NULL;
?>

------解决方案--------------------
存在数据库就跟平时修改文字一样做就可以...
当他是字串就可以了
------解决方案--------------------
header("content-type:image/jpeg"); 输出的前边加上这个
Statement
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
mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

mysql需要commit吗mysql需要commit吗Apr 27, 2022 pm 07:04 PM

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql-connector是什么mysql-connector是什么May 12, 2022 pm 04:04 PM

“mysql-connector”是mysql官方提供的驱动器,可以用于连接使用mysql;可利用“pip install mysql-connector”命令进行安装,利用“import mysql.connector”测试是否安装成功。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

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),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.