$base64_image_content:要保存的Base64php
$path:要保存的路径this
public function base64_image_content($base64_image_content,$path){ //匹配出图片的格式 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){ $type = $result[2]; //$ff=date('Y-m-d',time()); $new_file = $path."/lunbo/"; if(!file_exists($new_file)){ //检查是否有该文件夹,若是没有就建立,并给予最高权限 mkdir($new_file, 0700); } $picname=mt_rand(0,99).time().".{$type}"; $new_file = $new_file.$picname; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){ return "/Uploads/lunbo/".$picname; }else{ return false; } }else{ return false; } }
调用url
$data['pic'] = $this->base64_image_content($_POST['thumb_url'],'./Uploads');