Recently, I came into contact with a small app that uploads images to the server through a mobile phone. At first I was wondering how to use it to receive data. Finally, I found that php is used on the Internet: input. I checked the official introduction to php: input. Indeed, except in the case of enctypemultipartform-data, the stream cannot be received.
Recently, I came into contact with a small app that uploads images to the server through a mobile phone. At first I was wondering how to use it to receive data. Finally, I found that php is used on the Internet: // input. I checked the official introduction to php: // input. It is true that the stream cannot be received except when enctype = multipart/form-data.
Recently, I came into contact with a small app that uploads images to the server through a mobile phone. At first I was wondering how to use it to receive data. Finally, I found that php is used on the Internet: // input. I checked the official introduction to php: // input. It is true that the stream cannot be received except in the case of enctype = "multipart/form-data, in other cases. Now, I can write a test file locally.
Upload files
// @ File phpinput_post.php
Using data=file_get_contents('btn.png ');
$ Http_entity_body = $ data;
$ Http_entity_type = 'application/x-www-form-urlencoded ';
$ Http_entity_length = strlen ($ http_entity_body );
$ Host = '1970. 0.0.1 ';
$ Port = 80;
$ Path = '/image. php ';
$ Fp = fsockopen ($ host, $ port, $ error_no, $ error_desc, 30 );
If ($ fp ){
Fputs ($ fp, "POST {$ path} HTTP/1.1 \ r \ n ");
Fputs ($ fp, "Host: {$ host} \ r \ n ");
Fputs ($ fp, "Content-Type: {$ http_entity_type} \ r \ n ");
Fputs ($ fp, "Content-Length: {$ http_entity_length} \ r \ n ");
Fputs ($ fp, "Connection: close \ r \ n ");
Fputs ($ fp, $ http_entity_body. "\ r \ n ");
While (! Feof ($ fp )){
$ D. = fgets ($ fp, 4096 );
}
Fclose ($ fp );
Echo $ d;
}
?>
Receive files
/**
* Recieve image data
**/
Error_reporting (E_ALL );
Function get_contents (){
$ Xmlstr = file_get_contents ("php: // input ");
Using filename1_time().'.png ';
If (file_put_contents ($ filename, $ xmlstr )){
Echo 'success ';
} Else {
Echo 'failed ';
}
}
Get_contents ();
?>
Run test. php and you will be able to see the result. The result is successful. You will also see the file I just uploaded in the corresponding directory. If you are a server, you only need to receive the php code in the file. The upload I simulated above needs to be constructed based on the client, and I will connect it to the client later. So it can be used, and there may be some data problems during client upload, because we have various situations in this process.
Today, the company asked the APP to send an image to the PHP program for receiving and saving, and a lot of parameters are required in the middle!
I have never used apps to interact with PHP before. This is a challenge (for an intern without guidance)
1.app 1.jpg with two parameters: X and Y.
2.php is responsible for accepting x,yand 1.jpg, and saving 1.jpg to the server
Steps:
1. PHP Page code
$ Data = file_get_contents ('php: // input'); // you can obtain the original unprocessed data (keep the sent image stream intact ), use X # Y # image stream on APP to send http to PHP page
// Process and split the PHP page
2. Data Processing
Split data streams first
$ Vars = explode ("#", $ data, 3); // This prevents damage to the image stream from being split into three copies.
/* Save some code */
$ Img = $ vars [2];
$ Path = '/var/www/uploads /';
$ Newfilename = time().20..jpg ";
$ File = $ path. $ newfilename;
$ Handle = fopen ($ file, "w ");
If ($ handle) {fwrite ($ handle, $ img );
Fclose ($ handle );
}
^ _ ^: Check whether an image is generated in the/var/www/directory?