PHP implementation video camera upload avatar function Instance code

Source: Internet
Author: User
Tags http request

Now the mobile phone is very hot, so how to use the phone to take pictures and upload the avatar? The reason is very simple, is the data transmission, first mobile phone transmission photo information, this is not a post pass or get function pass,
This other data format is passed, using $globals [' Http_raw_post_data '], which is similar to POST, but $globals [' Http_raw_post_data '] supports a richer data format Details of the difference please yourself Baidu Google.
The design process is:
$GLOBALS [' http_raw_post_data '] transfer photo data stream (binary) –> open a blank chart-> write the data stream into a blank image-to determine if it is a valid picture-done.
Note that the binary stream of the photo transfer already contains the picture size, format, etc. attributes, time is the phone picture information passed to the web side page.

Mobile phone end of the general use of flash cut photo size, we all know that the picture and video is composed of binary stream, since the picture can be uploaded, then the video can upload it? The principle is not with the mobile phone photo upload avatar like it? I can study it.


Directly on the code

The code is as follows Copy Code

/*
* Upload Picture class
* @parameter; User ID
* @date: 2011-8-15
* @author: CY
* */
function Upimg ($subdir) {
Set Storage Path
if (!empty ($subdir)) {
$dirnow =GETCWD ();
$folder = $dirnow. " /www.111cn.net/". $subdir;/folder where to save images
if (!is_dir ($folder)) mkdir ($folder);

Picture naming
$datenow =date (' Ymd ');
$timenow =time ();
$image = $datenow. $timenow. JPG ';

//Picture exists
  $check = $folder. '/' . $image;
  if (file_exists ($check)) {
   unlink ($check);
  } else {
 & nbsp;  $png = file_get_contents (' Php://input ')? file_get_contents (' Php://input '): gzuncompress ($GLOBALS [' http_raw_post_data ']);/get POST binary raw data
 & Nbsp; if (!empty ($png)) {
     $file = @fopen ($folder.) /". $image," w ");
         fwrite ($file, $png);//write Www.111cn.net
     Fclose ($file);//close
    //orignal image location
     $write _image = $ Folder. '/' . $image;

}else{
$msg = "No Data Flow";
}
if (Getimageinfo ($write _image)) {
$msg = "Upload success";
}else{$msg = "Incorrect picture format";}
}
}else{
$msg = "parameter Error";
}
return $msg;
}

/*
* Detect whether the picture is legal
* @parameter; Filename
* @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 111cn.net legal pictures.
return false;
}

}

When using XML-RPC, the server gets the client data, mainly through the PHP input stream, rather than the $_post array

Based on the above several probes, we can make the following summary:
1,content-type value is application/x-www-form-urlencoded, PHP will fill the HTTP request body corresponding data into the array $_post, fill in the $_ The data in the post array is the result of UrlDecode () parsing. (In fact, in addition to the Content-type, there are multipart/form-data to represent the data is form data, later we introduce)
2,php://input data, as long as the Content-type is not multipart/form-data (this condition is described later). So PHP://input data is consistent with the HTTP entity body part data. The length of the consistent data in this section is specified by Content-length.
3, the $_post data is "consistent" with the Php://input data only when Content-type is application/x-www-form-urlencoded and the Submit method is a post method (quotes, indicating that they are inconsistent in format, Content). In other cases, they are inconsistent.
4,php://input cannot read $_get data. This is because the $_get data is written in the path field of the HTTP request header (header) as Query_path, rather than in the body portion of the HTTP request.

This also helps us understand why the XML_RPC server reads data through file_get_contents (' Php://input ', ' R '). Instead of reading from $_post, it is because the XML_RPC data specification is XML and its content-type is text/xml.

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.