上传部份的代码不是题目,主如果server端怎样才吸收到APP端的图片信息。在B/S架构下,能够直接经由过程form表单设置enctype="multipart/form-data",$_FILES数组中就有了图片信息。那末在C/S形式中,也是云云吗? (引荐进修:PHP视频教程)
解答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; }
以上就是php接口怎样传输图片的细致内容,更多请关注ki4网别的相干文章!