<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[个人原创]使用PHP的Socket来模拟RFC协议中的网页POST上传图片]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Sat, 25 Jun 2011 12:08:55 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	content-length:&nbsp;&nbsp;这个值是从--$boundary，&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;&quot;.rawurlencode($k).&quot;&#92;&quot;&#92;r&#92;n&#92;r&#92;n， ...文件内容。。。&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n结束。注意：<br/>最后的content的分割符也就是boundary还有两个--，表示结束。<br/><br/><br/>其实协议是一个规化，代码是一个实现，久而久之，规划成为了规则，如：RFC协议就是一个大家在开发浏览器必须遵守的协议（http的（RFC 1867）：让HTML表单可以提交文件。它对HTML表单的扩展），而也具有开放性，为此，各种程序都可以实现这个协议，程序为协议而生，协议为功能而用，下面我们来实现一个通过PHP的Socket来模拟一次浏览器上传一张图片的提交代码，如下：<br/><br/>模拟浏览器遵循的Http协议发送图片程序，boundary开始时有一个 --,结束时：也有有 --,真正结束后有&#92;r&#92;n&#92;r&#92;n：<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;?php
$img_file = &#039;resources/images/winter.jpg&#039;;
$argva =array(&#039;username&#039; =&gt; &#039;Jerry&#039;, &#039;password&#039; =&gt; &#039;123456&#039;);
echo postdata(&quot;http://u.levoo.boosh.com.cn/upimage.php?hatchdevid=123456&quot;,$argva,$img_file);

function postdata($posturl,$data=array(),$file=&#039;&#039;)&#123;
$url = parse_url($posturl);
$boundary = &quot;---------------------------&quot;.substr(md5(rand(0,32000)),0,10);
$boundary_2 = &quot;--$boundary&quot;;
$content = $encoded = &quot;&quot;;
if($data)&#123;
while (list($k,$v) = each($data))&#123;
$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;&quot;.rawurlencode($k).&quot;&#92;&quot;&#92;r&#92;n&#92;r&#92;n&quot;;
$encoded .= rawurlencode($v).&quot;&#92;r&#92;n&quot;;
&#125;
&#125;

if($file)&#123;
$ext=strrchr($file,&quot;.&quot;);
$type = &quot;image/jpeg&quot;;
switch($ext)&#123;
case &#039;.gif&#039;: $type = &quot;image/gif&quot;;
break;
case &#039;.jpg&#039;: $type = &quot;image/jpeg&quot;;
break;
case &#039;.png&#039;: $type = &quot;image/png&quot;;
break;
&#125;
$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;eggpic&#92;&quot;; filename=&#92;&quot;$file&#92;&quot;&#92;r&#92;nContent-Type: $type&#92;r&#92;n&#92;r&#92;n&quot;;
$content = join(&quot;&quot;, file($file));
$encoded.=$content.&quot;&#92;r&#92;n&quot;;
&#125;

$encoded .= &quot;&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n&quot;;
$length = strlen($encoded);

$fp = fsockopen($url[&#039;host&#039;],80);
if(!$fp) return &quot;Failed to open socket to $url[host]&quot;;

//fputs($fp, &quot;POST $url[path] HTTP/1.0&#92;r&#92;n&quot;);
fputs($fp, &quot;POST $posturl HTTP/1.0&#92;r&#92;n&quot;); #传一个Get参数可以取到，如用上面的$url[path]取不到get参数的hatchdevid值
fputs($fp, &quot;Host: $url[host]&#92;r&#92;n&quot;);
fputs($fp, &quot;Content-type: multipart/form-data; boundary=$boundary&#92;r&#92;n&quot;);
fputs($fp, &quot;Content-length: &quot;.$length.&quot;&#92;r&#92;n&quot;);
fputs($fp, &quot;Connection: close&#92;r&#92;n&#92;r&#92;n&quot;); 
fputs($fp, $encoded);

$line = fgets($fp,1024);
echo $line;

$results = &quot;&quot;;
$inheader = 1;
while(!feof($fp))&#123;
$line = fgets($fp,1024);
if($inheader &amp;&amp; ($line == &quot;&#92;r&#92;n&quot; &#124;&#124; $line == &quot;&#92;r&#92;r&#92;n&quot;))&#123;
$inheader = 0;
&#125;elseif(!$inheader)&#123;
$results .= $line;
&#125;
&#125;
fclose($fp);
//echo $encoded.&quot;&#92;n&quot;;
return $results;
&#125;
?&gt;
</textarea><br/><br/><br/>PHP接收发送过来的图片程序：<br/><textarea name="code" class="html" rows="15" cols="100">
 header(&quot;Content-type: text/html; charset=utf-8&quot;); 
 print_r($_FILES);
 print_r($_POST);
 ......
 </textarea><br/><br/>测试返回：<br/><textarea name="code" class="html" rows="15" cols="100">
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[imageaaa] =&gt; Array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[name] =&gt; Winter.jpg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[type] =&gt; image/jpeg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[tmp_name] =&gt; /tmp/phpTuioCN
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[error] =&gt; 0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[size] =&gt; 812247
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)

)
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[username] =&gt; Jerry
&nbsp;&nbsp;&nbsp;&nbsp;[password] =&gt; 123456
)
type is not exist,permision denied
</textarea><br/>为何出现这样的情况呢？查找代码片段imageaaa，这个修改为file即可，修改后如下所示，上传成功：<br/><textarea name="code" class="html" rows="15" cols="100">
 Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[file] =&gt; Array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[name] =&gt; Winter.jpg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[type] =&gt; image/jpeg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[tmp_name] =&gt; /tmp/phpnKnLy5
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[error] =&gt; 0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[size] =&gt; 812247
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)

)
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[username] =&gt; Jerry
&nbsp;&nbsp;&nbsp;&nbsp;[password] =&gt; 123456
)
Stored in: upload/Winter.jpg???,??????!&lt;script&gt;alert(&#039;???,??????,???!&#039;);history.go(-1);&lt;/script&gt;
</textarea><br/>说明是Ok的，查下upload目录下，果然是在这个图片的，证明PHP是完全可以通过fsocket来模拟Http上传图片协议来实现浏览器上传的。<br/><br/>下面贴出PHP上传图片的简单代码：<br/><textarea name="code" class="html" rows="15" cols="100">
&lt;?php
header(&quot;Content-type: text/html; charset=utf-8&quot;); 
print_r($_FILES);
print_r($_POST);
if ((($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/gif&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;#124;&amp;#124; ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpeg&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;#124;&amp;#124; ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;application/octet-stream&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;#124;&amp;#124; ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/pjpeg&quot;))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp; ($_FILES[&quot;file&quot;][&quot;size&quot;] &lt; 2000000))
&#123;
&nbsp;&nbsp;if ($_FILES[&quot;file&quot;][&quot;error&quot;] &gt; 0)
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;错误信息: &quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;&lt;br /&gt;&quot;;&nbsp;&nbsp; //如果上传过程中出现错误，提示出来错误信息
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;else
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;if (file_exists(&quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]))
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot; 此文件已存在，请换一个图片名称再上传. &quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;script&gt;alert(&#039;&quot;.$_FILES[&quot;file&quot;][&quot;name&quot;].&quot; 此文件已存在，请换一个图片名称再上传.&#039;);history.go(-1);&lt;/script&gt;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;else
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;],
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;Stored in: &quot; . &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;恭喜您，文件上传成功！&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;&lt;script&gt;alert(&#039;恭喜您，文件上传成功，请返回！&#039;);history.go(-1);&lt;/script&gt;&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;
&#125;else
&#123;
&nbsp;&nbsp;echo &quot;type is not exist,permision denied&quot;;
&#125;
?&gt;
</textarea><br/>同时，这块是Http上传协议，用C，C++，Java也是可以实现的解析该Http协议的，这样图片上传就可以实现跨语言传输了，呵。<br/>Write By:Jackxiang,回忆未来，向东，其实都是一个人哈，呵呵。<br/><br/>附录：<br/>1.代码分析极其图片内容偷换试验情况：<br/><textarea name="code" class="html" rows="15" cols="100">
if($data)&#123;
while (list($k,$v) = each($data))&#123;
$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;&quot;.rawurlencode($k).&quot;&#92;&quot;&#92;r&#92;n&#92;r&#92;n&quot;;
$encoded .= rawurlencode($v).&quot;&#92;r&#92;n&quot;;
&#125;
......//rawurlencode主要是模拟浏览器提交，把传入的数组组合成一个Http的Rfc之Post协议。

$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;file&#92;&quot;; filename=&#92;&quot;$file&#92;&quot;&#92;r&#92;nContent-Type: $type&#92;r&#92;n&#92;r&#92;n&quot;;
//$content = join(&quot;&quot;, file($file));
$content = &quot;This is a picture contents.&quot;;//这儿是完全可以进行替换图片内容 的，file返回图片内容为一个数组，后join就是一张图片的内容
$encoded.=$content.&quot;&#92;r&#92;n&quot;;
&#125;
$encoded .= &quot;&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n&quot;;
$length = strlen($encoded);
echo $encoded;
echo &quot;length=&quot;.$length.&quot;&#92;r&#92;n&quot;;
$fp = fsockopen($url[&#039;host&#039;],80);
</textarea><br/>执行查看其Rfc传输的协议：<br/><textarea name="code" class="html" rows="15" cols="100">
[root@jackxiang wenkongbao]# php file.php&nbsp;&nbsp; 
-----------------------------228499b553
Content-Disposition: form-data; name=&quot;username&quot;
Jerry
-----------------------------228499b553
Content-Disposition: form-data; name=&quot;password&quot;
123456
-----------------------------228499b553
Content-Disposition: form-data; name=&quot;file&quot;; filename=&quot;Winter.jpg&quot;
Content-Type: image/jpeg
This is a picture contents.
-----------------------------228499b553--
length=412
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[file] =&gt; Array
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[name] =&gt; Winter.jpg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[type] =&gt; image/jpeg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[tmp_name] =&gt; /tmp/phptZT32d
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[error] =&gt; 0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[size] =&gt; 29
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
)
Array
(
&nbsp;&nbsp;&nbsp;&nbsp;[username] =&gt; Jerry
&nbsp;&nbsp;&nbsp;&nbsp;[password] =&gt; 123456
)
Stored in: upload/Winter.jpg???,??????!&lt;script&gt;alert(&#039;???,??????,???!&#039;);history.go(-1);&lt;/script&gt;
</textarea><br/>查看图片：<br/><textarea name="code" class="html" rows="15" cols="100">
[root@jackxiang wenkongbao]# cat upload/Winter.jpg 
This is a picture contents.
</textarea><br/>2.$boundary_2和$boundary的认识：<br/><textarea name="code" class="html" rows="15" cols="100">
$boundary_2 = &quot;--$boundary&quot;;//多了--
......
......
......
$encoded .= &quot;&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n&quot;; //加上了--
</textarea><br/><br/><textarea name="code" class="html" rows="15" cols="100">
fputs($fp, &quot;POST $url[path] HTTP/1.0&#92;r&#92;n&quot;);
fputs($fp, &quot;Host: $url[host]&#92;r&#92;n&quot;);
fputs($fp, &quot;Content-type: multipart/form-data; boundary=$boundary&#92;r&#92;n&quot;);// 这儿是$boundary
fputs($fp, &quot;Content-length: &quot;.$length.&quot;&#92;r&#92;n&quot;);
fputs($fp, &quot;Connection: close&#92;r&#92;n&#92;r&#92;n&quot;);//这儿里面全是$boundary_2
fputs($fp, $encoded);
</textarea><br/><br/>3.代码boundary后追加--疑问：<br/>对于后面：加上了 --的一般浏览器上传通过抓包查看（$encoded .= &quot;&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n&quot;; //加上了--<br/>），上传协议通过Fiddler2抓包查看，发现果然在结尾也多得有一个--的符号，这块可能在see RFC2616中应该有特别描述这块：<br/><textarea name="code" class="html" rows="15" cols="100">
POST /postcgi HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQPinyin 689; TencentTraveler 4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727; msn OptimizedIE8;ZHCN)
Content-Type: multipart/form-data; boundary=---------------------------7db434180f1c
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Content-Length: 559
Host: act.qq.com
Pragma: no-cache
Cookie: ac=1,030,012; o_cookie=372647693; pgv_pvid=1178192826; pgv_flv=10.0; pgv_r_cookie=1141195738684; pt2gguin=o0372647693; comment_uin=372647693 %u56de%u5fc6%u672a%u6765; comment_skey=e6888c5c9c7df1ab2a0353a0ee997c77+%BB%D8%D2%E4%CE%B4%C0%B4; pp_visitkey=20763857487967539; pvid=1178192826; suid=6712511995; lv_irt_id=4b2ac9aae16fbfcf6b13b86ab1e03580; uin_cookie=372647693; euin_cookie=C17391AD3B3F7C7DA501BB46FFDE372C10A364C97D642DE0

-----------------------------7db434180f1c
Content-Disposition: form-data; name=&quot;name&quot;

jackxiang
-----------------------------7db434180f1c
Content-Disposition: form-data; name=&quot;fileField&quot;; filename=&quot;C:&#92;Documents and Settings&#92;jackxiang&#92;桌面&#92;Curl_Host.txt&quot;
Content-Type: text/plain

curl -l -H &quot;Host:qdd.act.qq.com&quot;&nbsp;&nbsp;http://172.24.18.104/wap/getfriends?uin=250138501&amp;pwd=b2b3e01785f326bcc68bd184cfbc1aec

-----------------------------7db434180f1c
Content-Disposition: form-data; name=&quot;button&quot;

提交
-----------------------------7db434180f1c--
</textarea><br/><br/>看抓包情况的上面两行，在提交下一行的-----------------------------7db434180f1c后面就有一个--：<br/>Content-Disposition: form-data; name=&quot;button&quot;<br/><br/>提交<br/>-----------------------------7db434180f1c--<br/><br/><br/><br/>同时参考来源也贴下：http://topic.csdn.net/u/20110619/23/7b0403de-c494-4592-9cc8-9b04f87ddb4a.html?98743<br/><br/>这个兄弟说得较为详细，新添加【2013-04-10】：http://blog.zhaojie.me/2011/03/html-form-file-uploading-programming.html<br/><br/><br/><br/>Add Time：2014-01-18 测试c和epoll多进程上传文本加强版本：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php&nbsp;&nbsp;
function postdata($posturl,$data=array(),$file=&#039;&#039;)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;$url = parse_url($posturl);&nbsp;&nbsp;
&nbsp;&nbsp;$boundary = &quot;---------------------------&quot;.substr(md5(rand(0,32000)),0,10);&nbsp;&nbsp;
&nbsp;&nbsp;$boundary_2 = &quot;--$boundary&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;$content = $encoded = &quot;&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;if($data)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;while (list($k,$v) = each($data))&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;&quot;.rawurlencode($k).&quot;&#92;&quot;&#92;r&#92;n&#92;r&#92;n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$encoded .= rawurlencode($v).&quot;&#92;r&#92;n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;

&nbsp;&nbsp;if($file)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$ext=strrchr($file,&quot;.&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$type = &quot;image/jpeg&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;switch($ext)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case &#039;.gif&#039;: $type = &quot;image/gif&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case &#039;.jpg&#039;: $type = &quot;image/jpeg&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case &#039;.png&#039;: $type = &quot;image/png&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case &#039;.txt&#039;: $type = &quot;text/plain&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$encoded .= $boundary_2.&quot;&#92;r&#92;nContent-Disposition: form-data; name=&#92;&quot;file&#92;&quot;; filename=&#92;&quot;$file&#92;&quot;&#92;r&#92;nContent-Type: $type&#92;r&#92;n&#92;r&#92;n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$content = join(&quot;&quot;, file($file));&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$encoded.=$content.&quot;&#92;r&#92;n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;echo $encoded;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;

&nbsp;&nbsp;$encoded .= &quot;&#92;r&#92;n&quot;.$boundary_2.&quot;--&#92;r&#92;n&#92;r&#92;n&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;$length = strlen($encoded);&nbsp;&nbsp;
&nbsp;&nbsp;$fp = fsockopen($url[&#039;host&#039;],$url[&#039;port&#039;]);&nbsp;&nbsp;
&nbsp;&nbsp;if(!$fp) return &quot;Failed to open socket to $url[host]&quot;;&nbsp;&nbsp;

&nbsp;&nbsp;fputs($fp, &quot;POST $url[path] HTTP/1.0&#92;r&#92;n&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;fputs($fp, &quot;Host: $url[host]&#92;r&#92;n&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;fputs($fp, &quot;Content-type: multipart/form-data; boundary=$boundary&#92;r&#92;n&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;fputs($fp, &quot;Content-length: &quot;.$length.&quot;&#92;r&#92;n&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;fputs($fp, &quot;Connection: close&#92;r&#92;n&#92;r&#92;n&quot;);&nbsp;&nbsp; 
&nbsp;&nbsp;fputs($fp, $encoded);&nbsp;&nbsp;

&nbsp;&nbsp;$line = fgets($fp,1024);&nbsp;&nbsp;
&nbsp;&nbsp;if (!eregi(&quot;^HTTP/1&#92;.. 200&quot;, $line)) return null;&nbsp;&nbsp;

&nbsp;&nbsp;$results = &quot;&quot;;&nbsp;&nbsp;
&nbsp;&nbsp;$inheader = 1;&nbsp;&nbsp;
&nbsp;&nbsp;while(!feof($fp))&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;$line = fgets($fp,1024);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;if($inheader &amp;&amp; ($line == &quot;&#92;r&#92;n&quot; &#124;&#124; $line == &quot;&#92;r&#92;r&#92;n&quot;))&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$inheader = 0;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;elseif(!$inheader)&#123;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$results .= $line;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;&#125;&nbsp;&nbsp;
&nbsp;&nbsp;fclose($fp);&nbsp;&nbsp;
&nbsp;&nbsp;return $results;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;
?&gt;&nbsp;&nbsp;

&lt;?php
$img_file = &#039;/tmp/jack.txt&#039;;&nbsp;&nbsp;
$argva =array(&#039;username&#039; =&gt; &#039;Jerry&#039;, &#039;password&#039; =&gt; &#039;123456&#039;);&nbsp;&nbsp;
echo postdata(&quot;http://localhost:6666/&quot;,$argva,$img_file);&nbsp;&nbsp;
</textarea><br/><br/>c服务器收到：<br/>Host: localhost<br/>Content-type: multipart/form-data; boundary=---------------------------16b5e60b80<br/>Content-length: 404<br/>Connection: close<br/><br/>-----------------------------16b5e60b80<br/>Content-Disposition: form-data; name=&quot;username&quot;<br/><br/>Jerry<br/>-----------------------------16b5e60b80<br/>Content-Disposition: form-data; name=&quot;password&quot;<br/><br/>123456<br/>-----------------------------16b5e60b80<br/>Content-Disposition: form-data; name=&quot;file&quot;; filename=&quot;/tmp/jack.txt&quot;<br/>Content-Type: text/plain<br/><br/>1234567 jjj efg<br/><br/><br/>-----------------------------16b5e60b80--<br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [个人原创]使用PHP的Socket来模拟RFC协议中的网页POST上传图片]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>