This article is an example of how PHP receives a file stream from the app side. Share to everyone for your reference, specific as follows:
Solution Idea:
1. The client many pictures carries on the circulation to upload, simultaneously with the parameter, uses the certain rule combination production data flow (picture data puts in the last)
2. Use the data stream for the transmission, the PHP service end uses file_get_content (' Php://input ') to receive
3. After transmission, the data stream is divided according to the rule, take out the final picture data, and write the picture file
Sample code:
The following code is to send a single picture and parameters php file, more than one picture can be called circular.
<?php
/* curl_post.php/
/Set the post address of the request, must be the domain name that contains the URL, cannot be the relative path
$url = ' http://www.xxx.com/post.php ';
$pic _data = file_get_contents ('./me.jpg ');
$data = [
' Username=chafang_ '. Rand (999),
' password= '. MD5 (' 123456 '),
' pic= ' => $pic _data, Here store picture data
];
Use ' ##### ' to segment the array
$strData = Implode (' ##### ', $data);
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
Set header file information as data stream output
curl_setopt ($curl, Curlopt_header, 0);
Sets the information obtained to return as a file stream, rather than 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);
Displays the return value of the Post
Echo ($data);
Receive Curl request PHP file, return JSON format
<?php
/* post.php *
/$content = file_get_contents ("Php://input");
$domain = ' http://www.xxx.com/';
Note that you need to have write permission
$filename = ' update/'. Time (). _ '. Rand (100000, 999999). JPG ';
$data = Explode (' ##### ', $content, 3);
$count = count ($data);
$result = [];
If the file is written to success 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 ' = ' number
$tmp = explode (' = ', $val, 2);
$result [$tmp [0]] = $tmp [1];
}
Combined picture Access address
$result [' pic '] = $domain. $filename;
}
echo Json_encode ($result);
More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP file Operation Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"
I hope this article will help you with the PHP program design.