php实现base64图片上传方式

php实现base64图片上传方式

本例子中没有采用File Post上传文件方式!原理一样,为了更加的理解base64 选择将其输出在文本域中,并提交至服务器!运用到项目中建议采用提交File方式。

html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>简单的html5 File测试 for pic2base64</title> 
<style> 
</style> 
<script> 
    window.onload = function(){ 
        var input = document.getElementById("demo_input"); 
        var result= document.getElementById("result"); 
        var img_area = document.getElementById("img_area"); 
        if ( typeof(FileReader) === 'undefined' ){
            result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!"; 
            input.setAttribute('disabled','disabled'); 
        }else{
            input.addEventListener('change',readFile,false);
        } 
    }
    function readFile(){
        var file = this.files[0]; 
        //这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件   
        if(!/image\/\w+/.test(file.type)){
            alert("请确保文件为图像类型"); 
            return false; 
        }
        var reader = new FileReader(); 
        reader.readAsDataURL(file); 
        console.log();
        reader.onload = function(e){ 
                result.innerHTML = this.result; 
                img_area.innerHTML = '<div class="sitetip">图片img标签展示:</div>![]('+this.result+')'; 
        }
    } 
</script> 
</head>

<body> 
    <form action="file.php" method="post">
    <input type="file" value="sdgsdg" id="demo_input" /> 
    <textarea name="img" id="result" rows=30 cols=300></textarea> 
    <p id="img_area"></p> 
    <input type="submit" value="提交">
</form>
</body> 
</html>

PHP功能块代码

<?php
/**
 * base64图片上传
 * @param $base64_img
 * @return array
 */
$base64_img = trim($_POST['img']);
$up_dir = './upload/';//存放在当前目录的upload文件夹下

if(!file_exists($up_dir)){
    mkdir($up_dir,0777);
}

if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
    $type = $result[2];
    if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
        $new_file = $up_dir.date('YmdHis_').'.'.$type;
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
            $img_path = str_replace('../../..', '', $new_file);
            echo '图片上传成功</br>![](' .$img_path. ')';
        }else{
                    echo '图片上传失败</br>';

        }
    }else{
        //文件类型错误
    echo '图片上传类型错误';
    }
    
}else{
    //文件错误
    echo '文件错误';
}


实例效果如下:


这里写图片描述

希望大家互相交流。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一套实用的渗透测试岗位面试题,你会吗? 1.拿到一个待检测的站,你觉得应该先做什么? 收集信息 whois、网站源...
    g0阅读 10,428评论 0 9
  • 文件上传时Web应用最为常见的功能之一,传统的文件上传需要定制一个特殊的form表单来上传文件,以上传图片为例,常...
    ramostear阅读 5,800评论 0 1
  • Bruce_Zhu 于 2017.5.16-* via.http://www.jb51.net/article/5...
    旅行的意义zxy阅读 3,486评论 0 1
  • 2017年1月23日,我成功的放弃了接近5天的学习计划和生活规划,完全没有遵守当时假期开始时设计的计划,执行率基本...
    SteveY阅读 1,436评论 0 0
  • 海棠社第94社 主题:佛 体裁:不限 文/寒霜 【原创】 佛 菩提树下坐弥陀,嗔怨痴贪一念过。 旭日伴钟追浩渺,彩...
    刘寒霜阅读 3,003评论 29 26

友情链接更多精彩内容