DS 当前系统的目录分隔符
THINK_PATH 框架系统目录
ROOT_PATH 框架应用根目录
uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID。
//********************************base64图片处理 public function base64_image_content($base64_image_content) { // $base64_image_content="data:image/jpeg;base64,iVBOxxxx" //匹配出图片的格式 if (preg_match(‘/^(data:\s*image\/(\w+);base64,)/‘, $base64_image_content, $result)) { $type = $result[2];//图片后缀 $new_file =ROOT_PATH . ‘public‘ . DS ; $url=‘/upimages/‘. date("Ymd", time()) . "/"; $new_file=$new_file.$url; if (!file_exists($new_file)) { //检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir($new_file, 0777,true); } $filename = time() . ‘_‘ . uniqid() . ".{$type}"; //文件名 $new_file = $new_file . $filename; $url=$url.$filename; //写入操作 if(file_put_contents($new_file, base64_decode(str_replace($result[1], ‘‘, $base64_image_content)))) { return $url; }else{ return false; } } else { return false; } }
//普通图片上传 public function upload() { header(‘Content-Type:application/json‘); if($_FILES){ $file = $_FILES[‘file‘]; $temp_arr = explode(".", $file[‘name‘]); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); $file_name = rand(1, 9999) . time() . ‘.‘ . $file_ext; $tmp_file = $file[‘tmp_name‘]; $error = $file[‘error‘]; if($error == 0){ $date_path = ‘/material/upload/‘.date(‘Ymd‘); $path_url = $date_path .‘/‘. $file_name; $dir_url = ROOT_PATH . ‘public‘ . $date_path; if(!file_exists($dir_url)){ mkdir($dir_url,0777,true); } $move_url = $dir_url . DS . $file_name; // $path_url = mb_convert_encoding($path_url,‘UTF-8‘,‘UTF-8‘); move_uploaded_file($tmp_file, $move_url); // return ‘{"code":1,"msg":"操作成功","data":"‘.$path_url.‘"}‘; return $path_url; } } return false; }
原文:https://www.cnblogs.com/ygyy/p/12204365.html