Question: When the APP uploads an avatar, how should PHP, as the API end, receive the image information?
#The upload part of the code is not a problem, the main problem is how the server side can receive the image information from the APP side. Under the B/S architecture, you can set enctype="multipart/form-data" directly through the form form, and the image information will be in the $_FILES array. So is this also true in C/S mode? (Recommended learning: PHP video tutorial)
Answer 1 (see method 1): Generally, binary stream transmission is used. The client transmits binary data, the server receives it, and then file_put_contents just writes the file. The file name format and where to put the file are all defined by yourself.
Answer 2 (see method 2): The Android or IOS client simulates an HTTP Post request to the server. After the server receives the corresponding Post request (obtain image resources through $_FILES ), return response information to the client. (This method is the same as the method of obtaining Html submission)
Base64 encrypt the image into a string for transmission
Instructions: IOS or Android side: Base64 encode the image to obtain a string, and pass it to the interface
Interface side: Base64 decode the received string, and then upload it to the specified location through the file_put_contents function
/** * 图片上传 * @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; }
The above is the detailed content of How to transfer pictures through php interface. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment