PHP simulates post upload image implementation code, _PHP tutorial

WBOY
Release: 2016-07-11 10:36:19
Original
1675 people have browsed it

PHP simulation post upload image implementation code,

The example in this article shares the specific code of PHP simulation post upload image for your reference, the specific content is as follows

Both server and client are in PHP language
But the client is not a web page and does not run on the browser, but on the command line
What we need to do now is to access the server on the client, read the image on the server, change the width of the image to 100 on the client, and then upload it to the server.
The first two steps have been completed:
1. Read the image on the server, convert it into binary and send it to the client. The client uses fopen and fwrite to regenerate the image and store it in the client's org/resouse directory
2. Then process the image in org/resouse to a width of 100 and store it in the client org/w100 directory
3. How to re-upload it to the server in the last step?

The first two steps have been completed and can be ignored
There is a picture in the org/w100/ directory of the client: 5k0ach.jpg. How to upload this picture to the server?
Note: The client is not a web page and does not have an interface such as a form. It runs on the command line
Part of the code of client gptest.php (the login part is omitted, assuming the login is successful, directly assign a value to psn_id):

<&#63;php 
$psn_id = "1fbahh"; 
$url = SERVER_URL . '/get_imginfo.php'; 
//SERVER_URL为我自己定义的常量,其值为:http://localhost:8080/phpClientSer 
$ans = postData_json($url, "psn_id=$psn_id");//postData_json()和postData()在check.php 
 
print_r($ans); 
 
if ($ans['count'] > 0) { 
 if (!file_exists("org")) { 
  mkdir("org"); 
  mkdir("org/resouse/"); //从服务器读取过来的原图片存放路径 
  mkdir("org/w100/"); //把上目录中临时存放的图片处理为宽度100后存放的路径 
  mkdir("org/temp/"); //出来gif图片是的临时mul 
 } 
 foreach ($ans['pdt_id'] as $k => $pdt_id) { 
  $img = "org/resouse/" . $pdt_id . $ans['img_style'][$k]; 
 
  $url = SERVER_URL . '/get_stream.php';//访问服务器的路径 
  $postString = $ans['img_url'][$k]; //传递的参数[服务器上图片的路径] 
  $stream = postData($url, "img_url=" . $ans['img_url'][$k]);//从服务器读取的图片内容 
  $file = fopen($img, "w+"); //打开文件准备写入 
  fwrite($file, $stream); //写入 
  fclose($file); //关闭 
 
  $image_resize = new image_resize(); 
  $image_resize->act($img, $pdt_id);//处理图片 
 
  $img_u = "org/w100/" . $pdt_id . $ans['img_style'][$k];//处理后图片的存放路径 
   
  //下面的代码是把处理过的图片转为二进制传到服务器,问题就出在这段代码 
  $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"; 
 } 
} 
&#63;> 

Copy after login

Part of 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); 
} 

Copy after login

Client files:


Double-click the bat.bat file to run pgtest.php on the command line


The file directory where the server handles client requests [http://localhost:8080/phpClientSer/]:

login.php Login
get_imginfo.php Gets the name, type [jpg/png/gif], path and other information of the image from the database after successful login
get_stream.php reads the image according to the image path:

$img_url = $_POST['img_url']; 
$stream = file_get_contents($img_url); 
echo $stream;
Copy after login

create_img.php receives the binary sent by 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+");//打开文件准备写入 
 
fwrite($file,$img_stm);//写入 
fclose($file);//关闭 
echo "ok";
Copy after login

New images created by the server cannot be opened:

The last 5 lines of code in client gptest.php and the code in server create_img.php need to be changed.

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Bangke Home.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1138984.htmlTechArticlePHP simulates post upload image implementation code. This article example shares the specific code of php simulates post upload image for everyone. For your reference, the specific content is as follows. Both the server and the client are ph...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!