文件以base64格式上传
大多数文件上传都是通过form表单传输过去的,php有个专门处理文件上传的函数 $_FILES ,当然了,form表单上传不是唯一的方法,也可以通过ajax传输进行处理,在该网站中的文件夹上传就是通过ajax传过去的,后台接收通过get或者post,而base64数据当然也是get或者post接收了,这时的图片转成的base64格式传到后台,后台通过函数base64_decode再转为图片,就可以进行保存在相应的目录。
if($_POST['datas']){ $imgages=$_POST['datas']; $name=$_POST['name']; $directory=Vpicture.'/'.$name; $dataname=array(); /*允许的图片类型 ----gif,jpg,jpeg,bmp,png--------*/ $fietype=array("data:image/gif;base64,","data:image/jpg;base64,","data:image/jpeg;base64,","data:image/bmp;base64,","data:image/png;base64,"); foreach ($imgages as $val){ $img = str_replace($fietype,'', $val); $img = str_replace(' ','+', $img); $img = base64_decode($img); /*获取文件类型*/ $str= substr($val , 0 , 30); /*获取分号前的字符串*/ $arr1=explode(";",$str); $arr=explode("/",$arr1[0]); $file_name = time().'_'.mt_rand().'.'.$arr[1]; $dataname[]=$file_name; $file = $directory.'/'.$file_name; $arr = file_put_contents($file, $img); if(!$arr){ $dataname="出错"; } }