最近一个app ios的 用thinkphp写接口 实名认证中 必须要上传一张自己的图片
如何用thinkphp 写上传图片的接口 有没有源码 请注明每步操作 万分感谢!!
回复内容: 最近一个app ios的 用thinkphp写接口 实名认证中 必须要上传一张自己的图片
如何用thinkphp 写上传图片的接口 有没有源码 请注明每步操作 万分感谢!!
//上传图片方法 可以放在父类以便以后继承直接调用
//两种上传方式一种是file另一种是base64
public function picupload(){ if (!is_post) { die(' smeta(base64) :
smeta :
'); } //base64上传方式(主要是为了处理微信不支持 input file) $smeta = $_post['smeta']; if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $smeta, $result)) {//base64上传 $data = base64_decode(str_replace($result[1], '', $smeta)); $dataname = './uploads/' . uniqid() . '.' . $result[2]; if (file_put_contents($dataname, $data)) { $this->ajaxoutput($dataname); //返回数据结构自行封装 }else{ $this->ajaxerror('上传出错'); } } //处理file上传 这里是调用thinkphp封装好\think\upload这个上传类 可以学习写thinkphp官方这个类是怎么写的 $config = array( 'rootpath' => './uploads/', 'savepath' => '', 'maxsize' => 11048576, 'savename' => array('uniqid', ''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autosub' => false, ); $upload = new \think\upload($config);// $info = $upload->upload(); //开始上传 if ($info) { //上传成功 $first = array_shift($info); if (!empty($first['url'])) { $url = $first['url']; } else { $url = c(tmpl_parse_string.__upload__) . $first['savename']; } $this->ajaxoutput($url); } else { //上传失败,返回错误 $this->ajaxerror($upload->geterror()); }}
app post上来
然后php中$_files接收,没有什么特殊的