Home>

It ’s hot to take pictures on mobile phones now,So how to use your phone to take pictures and upload avatars?the reason is simple,Is data transfer,First, the mobile phone passes the photo information.Neither pass by post nor get by function,This another data format is passed,Using $globals ["http_raw_post_data"],

If we want to implement video photography in PHP, we need to use the flash plugin. The actual photos taken by flash are then received through PHP's $globals ["http_raw_post_data"].Then save it as an image,Let me introduce it below.

$Globals ["http_raw_post_data"] is used. This system function is very similar to post, but $globals ["http_raw_post_data"] supports richer data formats.

Design Flow:$globals ["http_raw_post_data"] Pass photo data stream (binary)->open a blank image->write the data stream into a blank image-determine whether it is a valid image-complete.

Note that the binary stream passing the photo already contains the size of the photo,Format, etc.In time, the mobile phone image information is passed to the web page.

Mobile phones generally use flash to cut photo size.Everyone knows that pictures and videos are made up of binary streams,Now that the picture can be uploaded,Can the video be uploaded?Is it the same principle as taking a picture with a mobile phone and uploading an avatar?You can research it yourself.

code show as below

/*
 * Upload pictures
 * @parameter;user id
 * @date:2015-10-08
 * @author:cy
 * * /
function upimg ($subdir) {
//Set the storage path
if (! empty ($subdir)) {
 $dirnow=getcwd ();
 $folder=$dirnow. "/www.php100.com/".$subdir;//folder where to save images
 if (! is_dir ($folder)) mkdir ($folder);
//image name
 $datenow=date ("ymd");
 $timenow=time ();
 $image=$datenow. $timenow. ". jpg";
//whether the picture already exists
 $check=$folder. "/". $image;
 if (file_exists ($check)) {
 unlink ($check);
 } else {
 $png=file_get_contents ("php://input")?file_get_contents ("php://input"):gzuncompress ($globals ["http_raw_post_data"]);//Get the binary raw data from the post
 if (! empty ($png)) {
 [email protected] ($folder. "/". $image, "w");
   fwrite ($file, $png);//write to hzhuti.com
 fclose ($file);//Close
 //orignal image location
  $write_image=$folder. "/". $image;
 } else {
 $msg="No data stream";
 }
 if (getimageinfo ($write_image)) {
  $msg="Upload succeeded";
 } else {$msg="The image is malformed";}
 }
} else {
 $msg="Parameter error";
}
 return $msg;
 }
/*
 * Check if the picture is legal
 * @parameter;file name
 * @date:2011-8-15
 * @author:cy
 * * /
 function getimageinfo ($imagename="") {
  $imageinfo=getimagesize ($imagename);
  if ($imageinfo! == false) {
   $imagetype=strtolower (substr (image_type_to_extension ($imageinfo [2]), 1));
//$imagesize=filesize ($imageinfo);
   return $info=array ("width" =>$imageinfo [0], "height" =>$imageinfo [1], "type" =>$imagetype, "mine" =>$imageinfo ["mine "]);
  } else {
   //not a php100.com legal image
   return false;
  }
 }

When using xml-rpc, the server obtains client data, mainly through the PHP input stream input, not the $_post array

Based on the above probes,We can make the following summary:

1. When the content-type value is application/x-www-form-urlencoded, php will fill the corresponding data of the http request body into the array $_post, and the data filled into the $_post array is parsed by urldecode () the result of.(Actually, in addition to the content-type, there is multipart/form-data to indicate that the data is form data.We will introduce later)

2. php://input data, as long as the content-type is not multipart/form-data (this condition restriction will be introduced later). Then the php://input data is consistent with the data of the http entity body.The length of this part of the consistent data is specified by content-length.

3. Only when the content-type is application/x-www-form-urlencoded and the submission method is the post method, the $_post data and the php://input data are "consistent" (quoted,Indicates that they are inconsistent,The content is consistent). In other cases,They are all inconsistent.

4. php://input cannot read $_get data. It is because $_get data is written as query_path in the path field of the http request header, not in the body part of the http request.

It also helped us understand,Why does the xml_rpc server read data through file_get_contents ("php://input", "r"). Instead of reading from $_post, it is precisely because the xml_rpc data specification is xml, and its content-type is text/xml.

Effect picture:

php
  • Previous How to solve phpmyadmin import database file maximum limit of 2048KB
  • Next jQuery webpage right side ad follows scrolling code sharing