[PHP] phpbase64 上传图片 →→→→→进入此内容的聊天室

来自 , 2019-03-29, 写在 PHP, 查看 114 次.
URL http://www.code666.cn/view/5c80985b
  1. php实现base64图片上传方式实例代码
  2.  
  3. 本篇文章主要介绍了php实现base64图片上传方式实例代码,这里整理了详细的代码,具有一定的参考价值,有需要的小伙伴可以参考下。
  4. .
  5. .
  6.  
  7.  
  8. 本例子中没有采用File Post上传文件方式!原理一样,为了更加的理解base64 选择将其输出在文本域中,并提交至服务器!运用到项目中建议采用提交File方式。
  9.  
  10. html代码
  11.  
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>简单的html5 File测试 for pic2base64</title>
  17. <style>
  18. </style>
  19. <script>
  20.   window.onload = function(){
  21.     var input = document.getElementById("demo_input");
  22.     var result= document.getElementById("result");
  23.     var img_area = document.getElementById("img_area");
  24.     if ( typeof(FileReader) === 'undefined' ){
  25.       result.innerHTML = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!";
  26.       input.setAttribute('disabled','disabled');
  27.     }else{
  28.       input.addEventListener('change',readFile,false);
  29.     }
  30.   }
  31.   function readFile(){
  32.     var file = this.files[0];
  33.     //这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件  
  34.     if(!/image\/\w+/.test(file.type)){
  35.       alert("请确保文件为图像类型");
  36.       return false;
  37.     }
  38.     var reader = new FileReader();
  39.     reader.readAsDataURL(file);
  40.     console.log();
  41.     reader.onload = function(e){
  42.         result.innerHTML = this.result;
  43.         img_area.innerHTML = '<div class="sitetip">图片img标签展示:<img src="'+this.result+'"  alt="上海鲜花港 - 郁金香" /></div>![]('+this.result+')';
  44.     }
  45.   }
  46. </script>
  47. </head>
  48.  
  49. <body>
  50.   <form action="file.php" method="post">
  51.   <input type="file" value="sdgsdg" id="demo_input" />
  52.   <textarea name="img" id="result" rows=30 cols=300></textarea>
  53.   <p id="img_area"></p>
  54.   <input type="submit" value="提交">
  55. </form>
  56. </body>
  57. </html>
  58.  
  59.  
  60. PHP功能块代码
  61.  
  62. <?php
  63. /**
  64.  * base64图片上传
  65.  * @param $base64_img
  66.  * @return array
  67.  */
  68. $base64_img = trim($_POST['img']);
  69. $up_dir = './upload/';//存放在当前目录的upload文件夹下
  70.  
  71. if(!file_exists($up_dir)){
  72.   mkdir($up_dir,0777);
  73. }
  74.  
  75. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
  76.   $type = $result[2];
  77.   if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
  78.     $new_file = $up_dir.date('YmdHis_').'.'.$type;
  79.     if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
  80.       $img_path = str_replace('../../..', '', $new_file);
  81.       echo '图片上传成功</br>![](' .$img_path. ')';
  82.     }else{
  83.           echo '图片上传失败</br>';
  84.  
  85.     }
  86.   }else{
  87.     //文件类型错误
  88.   echo '图片上传类型错误';
  89.   }
  90.  
  91. }else{
  92.   //文件错误
  93.   echo '文件错误';
  94. }
  95.  
  96.  
  97. 实例效果如下:
  98.  

回复 "phpbase64 上传图片"

这儿你可以回复上面这条便签

PHP功能块代码 ![](' .$img_path. ')'; }else{ echo '图片上传失败
'; } }else{ //文件类型错误 echo '图片上传类型错误'; } }else{ //文件错误 echo '文件错误'; } 实例效果如下:
captcha