时间:2021-07-01 10:21:17 帮助过:18人阅读
测试上传文件
$base64=file_get_contents("php://input"); //获取输入流
$base64=json_decode($base64,1);
$data = $base64['base64'];
preg_match("/data:image\/(.*);base64,/",$data,$res);
$ext = $res[1];
if(!in_array($ext,array("jpg","jpeg","png","gif"))){
echo json_encode(array("error"=>1));die;
}
$file=time().'.'.$ext;
$data = preg_replace("/data:image\/(.*);base64,/","",$data);
if (file_put_contents('../uploadFiles/'.$file,base64_decode($data))===false) {
echo json_encode(array("error"=>1));
}else{
echo json_encode(array("error"=>0,'src'=>'../uploadFiles/'.$file));
}