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

base64编码的图片上传到服务器

WBOY
Release: 2016-06-07 11:37:21
Original
1560 people have browsed it

使用$.post提交表格时,图片是无法上传的(除非用编辑器插件什么的,那些都比较大)。
这是一个新思路,图片转base64编码,提交上去,当然,图片不能太大,超过上传限制是不行的。
function base64_upload($base64) {<br>     $base64_image = str_replace(' ', '+', $base64);<br>     //post的数据里面,加号会被替换为空格,需要重新替换回来,如果不是post的数据,则注释掉这一行<br>     if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){<br>         //匹配成功<br>         if($result[2] == 'jpeg'){<br>             $image_name = uniqid().'.jpg';<br>             //纯粹是看jpeg不爽才替换的<br>         }else{<br>             $image_name = uniqid().'.'.$result[2];<br>         }<br>         $image_file = "./upload/test/{$image_name}";<br>         //服务器文件存储路径<br>         if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){<br>             return $image_name;<br>         }else{<br>             return false;<br>         }<br>     }else{<br>         return false;<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

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!