您好,欢迎访问一九零五行业门户网

PHP关于AIP图片上传接口

php上传的简单案例:
html文件:
100db36a723c770d327fc0aef2ce13b1e26208a155f35e341554823f0c61e237     df3ebe81f9e006ca6394758e87d62466     9d4b78a146d367b9a903fbbd0e39da2af5a47148e367a6035fd7a2faa965022e73a6ac4ed44ffec12cee46588e518a5e
样式相关:
手机端,点击上传按钮,弹出相机:
<input type="file" accept="image/*;capture=camera">直接调用相机 <input type="file" accept="image/*" />调用相机 图片或者相册
php文件:
<?php$file = $_files['file'];//得到传输的数据 //得到文件名称$name = $file['name'];$type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型,并且都转化成小写$allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型 //判断文件类型是否被允许上传if(!in_array($type, $allow_type)){ //如果不被允许,则直接停止程序运行 return ; }//判断是否是通过http post上传的if(!is_uploaded_file($file['tmp_name'])){ //如果不是通过http post上传的 return ; }$upload_path = "./img/"; //上传文件的存放路径 //开始移动文件到相应的文件夹if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){ echo "successfully!"; }else{ echo "failed!"; }?>
使用thinkphp上传类上传的简单案例:
= 'maxsize' => 3145728, 'exts' => ('jpg', 'gif', 'png', 'jpeg'), 'rootpath' => './public/uploads/info/', 'savepath' => '', 'savename' => ('uniqid',''), 'autosub' => , 'subname' => ('date','ymd'), upload(['result'] = 1['imgurl'] = ''['msg'] = '' = = ->upconfig['rootpath'] . ->upconfig['savepath'(!( = (, 0777, (! ['result'] = 0['msg'] = "创建保存图片的路径失败!" = \think\upload(->
(! ['result'] = 0['msg'] = -> = ->upconfig['rootpath'] . ['savepath'].['savename' = ('./', '/', ['result'] = 1['imgurl'] = (0 = ->upload(['attorney']);
移动端app上传图片实例:api接口:
问题:app上传头像,php作为api端应该如何接收图片信息?
上传部分的代码不是问题,主要是server端如何才能接收到app端的图片信息。在b/s架构下,可以直接通过form表单设置enctype="multipart/form-data",$_files数组中就有了图片信息。那么在c/s模式中,也是如此吗?
解答1(见方式一): 一般是采用二进制流传输,客户端传的是二进制,服务器端接收,然后file_put_contents写入文件就可以了。文件名格式,文件放哪里,这些自己定义。
解答2(见方式二):android或者ios客户端模拟一个http的post请求到服务器端,服务器端接收相应的post请求后(通过$_files获取图片资源),返回响应信息给给客户端。(这一种方式和获取html方式提交的方法一样)
方式一:把图片进行base64加密成字符串,进行传输
说明:ios或者安卓端:通过把图片进行base64编码得到字符串,传给接口
接口端:把接收的字符串进行base64解码,再通过file_put_contents函数,上传到指定的位置
/** * 图片上传 * @param $imginfo - 图片的资源,数组类型。['图片类型','图片大小','图片进行base64加密后的字符串'] * @param $companyid - 公司id * @return mixed */ public function uploadimage( $imginfo , $companyid ) { $image_type = strip_tags($imginfo[0]); //图片类型 $image_size = intval($imginfo[1]); //图片大小 $image_base64_content = strip_tags($imginfo[2]); //图片进行base64编码后的字符串 $upload = new uploaderservice(); $upconfig = $upload->upconfig; if(($image_size > $upconfig['maxsize']) || ($image_size == 0)) { $array['status'] = 13; $array['comment'] = "图片大小不符合要求!"; return $array; } if(!in_array($image_type,$upconfig['exts'])) { $array['status'] = 14; $array['comment'] = "图片格式不符合要求!"; return $array; } // 设置附件上传子目录 $savepath = 'bus/group/' . $companyid . '/'; $upload->upconfig['savepath'] = $savepath; //图片保存的名称 $new_imgname = uniqid().mt_rand(100,999).'.'.$image_type; //base64解码后的图片字符串 $string_image_content = base64_decode($image_base64_content); // 保存上传的文件 $array = $upload->upload($string_image_content,$new_imgname); return $array; }
// 上传配置信息 public $upconfig = array( 'maxsize' => 3145728, //3145728b(字节) = 3m 'exts' => array('jpg', 'gif', 'png', 'jpeg'),// 'rootpath' => './public/uploads/info/', 'rootpath' => 'https://www.eyuebus.com/public/uploads/info/', ); /** * @param $string_image_content - 所要上传图片的字符串资源 * @param $new_imgname - 图片的名称,如:57c14e197e2d1744.jpg * @return mixed */ public function upload($string_image_content,$new_imgname) { $res['result'] = 1; $res['imgurl'] = ''; $res['comment'] = ''; do { $ret = true; $fullpath = $this->upconfig['rootpath'] . $this->upconfig['savepath']; if(!file_exists($fullpath)){ $ret = mkdir($fullpath, 0777, true); } if(!$ret) { // 上传错误提示错误信息 $res['result'] = 12; $res['comment'] = "创建保存图片的路径失败!"; return $res; break; } //开始上传 if (file_put_contents($fullpath.$new_imgname, $string_image_content)){ // 上传成功 获取上传文件信息 $res['result'] = 0; $res['comment'] = "上传成功!"; $res['imgname'] = $new_imgname; }else { // 上传错误提示错误信息 $res['result'] = 11; $res['comment'] = "上传失败!"; } } while(0); return $res; }
方式二:android或者ios客户端模拟一个http的post请求到服务器端,服务器端接收相应的post请求后(通过$_files获取图片资源),返回响应信息给给客户端。(这一种方式和获取html方式提交的方法一样)
移动端需要请求一个url,这个url接收post过去的数据,比如:http://www.apixxx.net/home/uploader/uploadprepare
public function uploadprepare() { $array = array(); $post_log = print_r($_post, true); log::record($post_log, 'debug'); $file_log = print_r($_files, true); log::record($file_log, 'debug'); $token = $_post['token']; $token_str = jwt_decode($token);$user_type = $token_str['user_type']; // 设置附件上传子目录 if($user_type == 1) { $savepath = 'travel/group/' . $user_companyid . '/'; }elseif ($user_type == 2) { $savepath = 'bus/group/' . $user_companyid . '/'; }elseif ($user_type == 3) { $savepath = 'driver/group/' . $user_companyid . '/'; }else { $array['status'] = 3; $array['comment'] = '非法用户!'; return $array; } $this->upconfig['savepath'] = $savepath; // 保存上传的文件(单张) // $res = $this->upload($_files['file']); // 保存上传的文件(多张) 移动端的表单name=“xxx[]”,支持多张图片 $res = $this->upload(); $array['status'] = $res['status']; $array['comment'] = $res['comment']; $array['responseparameters']['img_url'] = $res['img_url']; echo json_encode($array); } protected function upload() { $res['status'] = 1; $res['imgurl'] = ''; $res['comment'] = ''; do { $ret = true; $fullpath = $this->upconfig['rootpath'] . $this->upconfig['savepath']; if(!file_exists($fullpath)){ $ret = mkdir($fullpath, 0777, true); } if(!$ret) { // 上传错误提示错误信息 $res['status'] = 1; $res['comment'] = "创建保存图片的路径失败!"; break; } // 实例化上传类 $upload = new \think\upload($this->upconfig);// // 上传单个文件 // $info = $upload->uploadone($file); // 上传多个文件 $infos = $upload->upload(); // 上传的图片数量 $file_count = 0; foreach ($_files as $file_k => $file_v) { foreach ($file_v["size"] as $k => $v) { if($v == 0) { continue; } $file_count += 1; } } log::record("info_log", 'debug'); $info_log = print_r($infos,true); log::record($info_log, 'debug'); if(!$infos) { // 上传错误提示错误信息 $res['status'] = 2; $res['comment'] = $upload->geterror(); } else { // 获取的上传成功的图片数量 $info_count = 0; // 上传成功 获取上传文件信息 foreach($infos as $k => $v) { $imgurl[$v['key']][] = str_replace('./', '/', $this->upconfig['rootpath'] . $v['savepath'].$v['savename']); $info_count += 1; } if($file_count != $info_count) { $res['status'] = 1; $res['comment'] = "上传失败!上传的多张图片,没有全部上传成功"; }else { $res['status'] = 0; $res['comment'] = "上传成功!"; $res['img_url'] = $imgurl; } } } while(0); return $res; }
相关推荐:
相关推荐:
php 图片上传
图片和传真查看器 php 图片上传代码
php 图片上传代码_php教程
以上就是php关于aip图片上传接口的详细内容。
其它类似信息

推荐信息