Php simulates post Upload. The image uploading server and client are both in the php language.
However, the client is not running on a webpage, not on a browser, but on a command line.
What I want 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 ):
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
Reply to discussion (solution)
$ File = fopen ($ img_url, "wb ")
Binary writing
$ File = fopen ($ img_url, "wb ")
Binary writing
Thank you, but the question first is here: postData (), where the code for passing files is the key, and the second is to receive
If the passed binary file is not a correct binary file, it will not help to receive it.
$ File = fopen ($ img_url, "wb ")
Binary writing
Thank you. the problem has been solved. But there is no question about $ file = fopen ($ img_url, "wb"), but the method of passing data through postData ().
Reference http://bbs.csdn.net/topics/370023047
$ File = fopen ($ img_url, "wb ")
Binary writing
Thank you. the problem has been solved. But there is no question about $ file = fopen ($ img_url, "wb"), but the method of passing data through postData ().
Reference http://bbs.csdn.net/topics/370023047
Mime file header?