Php simulates post upload image implementation code,
The example in this article shares with you the specific code for simulating post upload images in php for your reference. The details are as follows:
Both the server and client are in the php language.
However, the client is not running on a webpage, not on a browser, but on a command line.
What we need to do now is access the server on the client, read the images on the server, change the image width to 100 on the client, and upload the images to the server.
The first two steps have been completed:
1. Read the images on the server and convert them to binary data to be uploaded to the client. The client uses fopen and fwrite to regenerate the images and store them in the org/resouse directory of the client.
2. process images in org/resouse as 100 in width and store them in the org/w100 directory of the client.
3. How can I upload it to the server again in the last step?
The first two steps have been completed. You can ignore them.
There is an image in the org/w100/directory of the Client: 5k0ach.jpg. How can I upload this image to the server?
Note:The client is not a web page and does not have a form or other interfaces. It runs on the command line.
Part of the client gptest. php code (skip the login part. If the login is successful, assign a value to psn_id directly ):
<? Php $ psn_id = "1 fbahh"; $ url = SERVER_URL. '/get_imginfo.php'; // SERVER_URL is a constant defined by me. Its value is http: // localhost: 8080/phpClientSer $ ans = postData_json ($ url, "psn_id = $ psn_id"); // postData_json () and postData () in check. php print_r ($ ans); if ($ ans ['Count']> 0) {if (! File_exists ("org") {mkdir ("org"); mkdir ("org/resouse /"); // Original Image Storage path read from the server mkdir ("org/w100 /"); // process the image temporarily stored in the directory as the mkdir ("org/temp/") path after the width is 100 /"); // The GIF image is temporary mul} foreach ($ ans ['pdt _ id'] as $ k => $ pdt_id) {$ img = "org/resouse /". $ pdt_id. $ ans ['img _ style'] [$ k]; $ url = SERVER_URL. '/get_stream.php'; // server access path $ postString = $ ans ['img _ url'] [$ k]; // The passed parameter [server slice path] $ stream = postData ($ url, "img_url = ". $ Ans ['img _ url'] [$ k]); // The image content read from the server $ file = fopen ($ img, "w + "); // open the file and prepare to write fwrite ($ file, $ stream); // write fclose ($ file); // close $ image_resize = new image_resize (); $ image_resize-> act ($ img, $ pdt_id); // process the image $ img_u = "org/w100 /". $ pdt_id. $ ans ['img _ style'] [$ k]; // the path for storing the processed image // the following code transfers the processed image to the server in binary format, the problem lies in the Code $ stm = file_get_contents ($ img_u); $ url = SERVER_URL. '/create_img.php'; $ postString = "pdt _ Id = $ pdt_id & img_style = ". $ ans ['img _ style'] [$ k]. "& img_stm = ". $ stm; $ move = postData ($ url, $ postString); echo "result ---------". $ move. "\ r \ n" ;}}?>
Check. php code
function postData($remote_server, $post_string) { $context = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . '\r\n' . 'User-Agent : Jimmy\'s POST Example beta' . '\r\n' . 'Content-length:' . strlen($post_string) + 8, 'content' => $post_string) ); $stream_context = stream_context_create($context); $data = file_get_contents($remote_server, false, $stream_context); return $data; } function postData_json($remote_server, $post_string) { $context = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . '\r\n' . 'User-Agent : Jimmy\'s POST Example beta' . '\r\n' . 'Content-length:' . strlen($post_string) + 8, 'content' => $post_string) ); $stream_context = stream_context_create($context); $data = file_get_contents($remote_server, false, $stream_context); return json_decode($data, true); }
Client files:
Double-click the bat. bat file to run pgtest. php on the command line.
The file directory for the server to process client requests [http: // localhost: 8080/phpClientSer/]:
Login. php login
After successful login, get_imginfo.php obtains the image name, type [jpg/png/gif], path, and other information from the database.
Get_stream.php:
$img_url = $_POST['img_url']; $stream = file_get_contents($img_url); echo $stream;
Create_img.php receives the binary data sent from the client and creates a new image:
$ Img_stm = $ _ POST ['img _ stm ']; $ pdt_id = $ _ POST ['pdt _ id']; $ img_style = $ _ POST ['img _ style']; $ img_url = $ _ SERVER ['document _ root']. "upload2/w100 /". $ pdt_id. $ img_style; $ file = fopen ($ img_url, "w +"); // open the file and write it into fwrite ($ file, $ img_stm ); // write fclose ($ file); // close echo "OK ";
New images created on the server cannot be opened:
The last five lines of client gptest. php code and the code of server create_img.php should be modified.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.