Sample code for PHP to receive an app-side send file stream

Source: Internet
Author: User
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); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.