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

来自 , 2021-01-09, 写在 PHP, 查看 107 次.
URL http://www.code666.cn/view/384babc3
  1. /**
  2.  * base64图片上传
  3.  * @param $base64_img
  4.  * @return array
  5.  */
  6. private static function img_upload($base64_img){
  7.     $base64_img = trim($base64_img);
  8.     $up_dir = '../../../upload/images/interface/';
  9.  
  10.     if(!file_exists($up_dir)){
  11.         mkdir($up_dir,0777);
  12.     }
  13.  
  14.     if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
  15.         $type = $result[2];
  16.         if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
  17.             $new_file = $up_dir.date('YmdHis_').method::getRandChar().'.'.$type;
  18.             if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
  19.                 $img_path = str_replace('../../..', '', $new_file);
  20.                 return array('code' => 1, 'msg' => "图片上传成功", 'url' => $img_path);
  21.             }
  22.             return array('code' => 2, 'msg' => "图片上传失败");
  23.         }
  24.         //文件类型错误
  25.         return array('code' => 4, 'msg' => "文件类型错误");
  26.     }
  27.     //文件错误
  28.     return array('code' => 3, 'msg' => "文件错误");
  29. }

回复 "function上传图片 方法"

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

captcha