php模拟post上传图片

服务器和客户端都是php语言
但是客户端不是网页,不在浏览器上运行,而是在命令行运行

我现在要做的是在客户端访问服务器,读取服务器上的图片,在客户端把图片的宽度变为100,然后再上传到服务器。
前两步都已完成:
1、读取服务器上的图片,转为二进制传到客户端,客户端用fopen、fwrite重新生成图片存放到客户端org/resouse目录下
2、再把org/resouse中的图片处理为宽度100存放到客户端org/w100目录下
3、 最后一步要怎样重新把它上传到服务器呢?

前两步已经完成,可以忽略
客户端org/w100/目录下有图片:5k0ach.jpg,要怎样把这张图片上传到服务器?

注意: 客户端不是网页,没有表单之类的界面,是在命令行运行的
客户端gptest.php的部分代码(省略登陆部分的,假设登陆成功,直接为psn_id赋值):
PHP code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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" ;
     }
}
?>

check.php部分代码
PHP code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
}

客户端文件:

双击bat.bat文件就会在命令行运行pgtest.php


服务器处理客户端请求的文件目录[http://localhost:8080/phpClientSer/]:

login.php  登陆
get_imginfo.php  登陆成功后从数据库获取图片的名称、类型[jpg/png/gif]、路径等信息
get_stream.php  根据图片路径读取图片:
PHP code
?
1
2
3
$img_url  $_POST [ 'img_url' ];
$stream  file_get_contents ( $img_url );
echo  $stream ;

create_img.php  接收客户端发送过来的二进制,创建新的图片:
PHP code
?
1
2
3
4
5
6
7
8
9
10
$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" ;

服务器创建的新图片打不开:


客户端gptest.php最后5行代码和服务器create_img.php的代码要改
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值