//路径以/市id/县公司id/专卖证号/照片名称格式
$image = file_get_contents('./cityid/1.txt'); //图片base64码
$path = "./cityid/area_company_id/411302125124/"; //文件保存地址
$file_name = 'photo'; //文件名称,不包含后缀
$url = base64_image_content($image,$path,$file_name);
echo $url;
/* base64格式编码转换为图片并保存对应文件夹 */
function base64_image_content($base64_image_content,$path,$file_name){
//匹配出图片的格式
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
$type = $result[2];
$new_file = $path;
if(!file_exists($new_file)){
//检查是否有该文件夹,如果没有就创建,并给予最高权限
mkdir($new_file, 0777,true);
}
$new_file = $new_file.$file_name.".{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
return '/'.$new_file;
}else{
return false;
}
}else{
return false;
}
}