(实用篇)PHP经过base64方式上传照片
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_gif/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVKjAEibODPROppZFOibrk1icABIyl3sngWbD10O2dX4IlgEpjKuDib361Og/0?tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p>付1元加群<strong style="color: blue;">php自学与交流QQ群</strong><span style="color: black;">:</span><span style="color: black;"><strong style="color: blue;">435916459,谢谢支持</strong></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">极客学院<span style="color: black;">教育</span>视频</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">想要领取<span style="color: black;">极客学院“<strong style="color: blue;"><span style="color: black;">整套学习视频教程</span></strong>”</span>(<span style="color: black;">所说</span>整套<span style="color: black;">便是</span>从前端到后端的<span style="color: black;">基本</span>视频教程,比较适合新手)的<span style="color: black;">朋友</span>,<span style="color: black;">能够</span>加</span><span style="color: black;">QQ </span><span style="color: black;"><strong style="color: blue;">2084533608</strong></span><span style="color: black;"><strong style="color: blue;">,领取视频教程,</strong><span style="color: black;">加入时验证信息填:</span><span style="color: black;"><strong style="color: blue;">学习</strong></span></span></p>
</span><span style="color: black;"><span style="color: black;"><img src="http://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVnj8V1o3nfiarx9aGcnicHbInM978huLf3ia2Vnd6Jtl9ouSpia95NzDtCA/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVkse7wHp5YAo0Q2KwVPYumjbuYU99bibicCKH5EjpQyJUNDPdIV78Ywibw/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">欢迎关注:<span style="color: black;">容易</span>学PHP</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">“<strong style="color: blue;"><span style="color: black;">容易</span>学PHP</strong>”,有<span style="color: black;">学习文档</span>,框架<span style="color: black;">视频教程</span>,<span style="color: black;">网站教程</span>,<span style="color: black;">供给</span>百度分享给<span style="color: black;">大众</span>下载,<span style="color: black;">保藏</span>。<span style="color: black;">大众</span>关注后回复关键词,便可获取!平台将与<span style="color: black;">大众</span>分享前端到后端的实例,学习语法,学习教程,技巧等!欢迎<span style="color: black;">大众</span>关注!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/QibLP1rpwH8v2Yo5tzSas9cfKYz54WdSNwC6QRicxDJPdveXO1diaS3JaFSoGB5eR58J29kpIicX56nia4y8NeTIPicA/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><img src="http://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVLwpMaDlpdnibb4ScDO0Wm8GB4LuoR1UJ9zKPSvLjZyGtG8pKwWF4licg/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVbXvRmYH7iaaia6VicUX3MZy2IQslKQt4ZEzBaqFxSQnzQL98OK0QBXp4g/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_gif/QibLP1rpwH8sk9yZ3LEWju87RQhcGD7JVdtopBic5bXpJX6tlBWmNkdpicydFfQB4SqEOyAicbf0WOViaQPsHv4Tx7w/0?tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">文案</span>正文内容<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">能够</span>参考一下</p><span style="color: black;"><?php</span><span style="color: black;">/**</span> <span style="color: black;">* base64<span style="color: black;">照片</span>上传</span> <span style="color: black;">* @param $base64_img</span> <span style="color: black;">* @return array</span> <span style="color: black;">*/</span><span style="color: black;">private</span> <span style="color: black;">static</span> <span style="color: black;">function</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> img_upload($base64_img){</p> $base64_img <span style="color: black;">=</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> trim($base64_img);</p> $up_dir <span style="color: black;">=</span> <span style="color: black;">../../../upload/images/interface/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">if</span>(<span style="color: black;">!</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file_exists($up_dir)){</p>mkdir($up_dir,<span style="color: black;">0777</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">if</span>(preg_match(<span style="color: black;">/^(data:\s*image\/(\w+);base64,)/</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, $base64_img, $result)){</p> $type <span style="color: black;">=</span> $result[<span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">];</p> <span style="color: black;">if</span>(in_array($type,<span style="color: black;">array</span>(<span style="color: black;">pjpeg</span>,<span style="color: black;">jpeg</span>,<span style="color: black;">jpg</span>,<span style="color: black;">gif</span>,<span style="color: black;">bmp</span>,<span style="color: black;">png</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">))){</p> $new_file <span style="color: black;">=</span> $up_dir.date(<span style="color: black;">YmdHis_</span>).method::getRandChar().<span style="color: black;">.</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$type;</p> <span style="color: black;">if</span>(file_put_contents($new_file, base64_decode(str_replace($result[<span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">], , $base64_img)))){</p> $img_path <span style="color: black;">=</span> str_replace(<span style="color: black;">../../..</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, , $new_file);</p> return <span style="color: black;">array</span>(<span style="color: black;">code</span> <span style="color: black;">=></span> <span style="color: black;">1</span>, <span style="color: black;">msg</span> <span style="color: black;">=></span> <span style="color: black;">"<span style="color: black;">照片</span>上传成功"</span>, <span style="color: black;">url</span> <span style="color: black;">=></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> $img_path);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> return <span style="color: black;">array</span>(<span style="color: black;">code</span> <span style="color: black;">=></span> <span style="color: black;">2</span>, <span style="color: black;">msg</span> <span style="color: black;">=></span> <span style="color: black;">"<span style="color: black;">照片</span>上传失败"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">//文件类型错误</span>return<span style="color: black;">array</span>(<span style="color: black;">code</span> <span style="color: black;">=></span> <span style="color: black;">4</span>, <span style="color: black;">msg</span> <span style="color: black;">=></span> <span style="color: black;">"文件类型错误"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">//文件错误</span> return <span style="color: black;">array</span>(<span style="color: black;">code</span> <span style="color: black;">=></span> <span style="color: black;">3</span>, <span style="color: black;">msg</span> <span style="color: black;">=></span> <span style="color: black;">"文件错误"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">博主</span>的书店</strong></span>:<span style="color: black;">https://weidian.com/?userid=518924181</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz/QibLP1rpwH8srtSicKnJLDzujn2dRhmc2nvqziamdvTia2Spkj8ZbIOvAWmAWWzCha73cMpM4OfqKx5SYl8Nn9HfZQ/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">点击上面<span style="color: black;">微X</span>号关注我</span><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">关注我哟</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">博主</span> </span><span style="color: black;"><span style="color: black;">定时</span>推送</span><span style="color: black;">PHP实例</span><span style="color: black;"><span style="color: black;">,<span style="color: black;">PHP语法</span><span style="color: black;">,<span style="color: black;">MySQL教程</span>,<span style="color: black;">Linux教程</span>,<span style="color: black;">学习<span style="color: black;">办法</span></span></span></span>等<span style="color: black;">许多</span><span style="color: black;">优秀</span>内容,<span style="color: black;">最接地气</span>、<span style="color: black;">重服务</span>的本地<span style="color: black;">微X</span>平台!关注<span style="color: black;">咱们</span>妥妥没错!(商务合作QQ:<strong style="color: blue;"><span style="color: black;">2230304070</span></strong>)</span></span></p>
同意、说得对、没错、我也是这么想的等。 我完全同意你的看法,期待我们能深入探讨这个问题。
页:
[1]