This article mainly introduces the PHP receive app side send file stream method, involving PHP based on Curl file transfer operation related skills, the need for friends can refer to the following
The example in this article describes how PHP receives the file stream from the app side. Share to everyone for your reference, as follows:
Solution Ideas:
1. Multiple images of the client loop upload, accompanied by parameters, and a certain combination of rules production data flow (image data at the end)
2. Using the data stream for transmission, PHP service End with file_get_content (' Php://input ') to receive
3. After the transfer, the data stream is separated by the rule, the final picture data is taken, and the picture file is written.
Example code:
The following code is to send a single picture and parameters php file, multiple pictures can be called loop.
<?php/* curl_post.php *///Set the post address of the request, must be the domain name that contains the URL, cannot be a relative path to the ' http://www.xxx.com/post.php '; $pic _data = File_get _contents ('./me.jpg '); $data = [ ' Username=chafang_ '. Rand (999), ' password= '. MD5 (' 123456 '), ' pic= ' = = $pic _data,//Store picture data here];//use ' ##### ' to split an array $strdata = implode (' ##### ', $data); $curl = Curl_init (); curl_setopt ($cur L, Curlopt_url, $url);//Set the header file information as the data stream output curl_setopt ($curl, Curlopt_header, 0);//Set the information obtained is returned as a file stream, rather than as a direct output. curl_setopt ($curl, Curlopt_returntransfer, 1);//Set POST mode to submit curl_setopt ($curl, Curlopt_post, 1); curl_setopt ($curl, Curlopt_postfields, $strData); $data = Curl_exec ($curl); Curl_close ($curl);//Display the return value of post echo ($data);
PHP file that receives the Curl request and returns the JSON format
<?php/* post.php */$content = file_get_contents ("Php://input"); $domain = ' http://www.xxx.com/';//note there is a need for write permission $filename = ' update/'. Time (). ' _ '. Rand (100000, 999999). JPG '; $data = explode (' ##### ', $content, 3), $count = count ($data); $result = [];//If the file is written successfully if (File_put_contents ($filename , $data [$count-1]) {//delete the last element in the data (that is, the picture) unset ($data [$count-1]); foreach ($data as $val) {//return parameter, and parameter value cannot exist ' = ' $tmp = explode (' = ', $val, 2); $result [$tmp [0]] = $tmp [1]; }//combination image access address $result [' pic '] = $domain. $filename;} echo Json_encode ($result);