php curl fsockopen 上传文件

用curl上传文件的话很方便,什么header,post串都不用生成了,用fsockopen要写一堆
curl:
Php代码  
  1. $file = array("upimg"=>"@E:/png.png");//文件路径,前面要加@,表明是文件上传.  
  2. $curl = curl_init("http://localhost/a.php");  
  3. curl_setopt($curl,CURLOPT_POST,true);  
  4. curl_setopt($curl,CURLOPT_POSTFIELDS,$file);  
  5. curl_exec($curl);  

fsockopen
Php代码  
  1. $uploadFile = file_get_contents("E:/png.png");  
  2. $boundary   = md5(time());  
  3.   
  4. $postStr .="--".$boundary."\r\n";//边界开始,注意默认比header定义的boundary多两个'-'  
  5. $postStr .="Content-Disposition: form-data; name=\"upimg\"; filename=\"E:/png.png\"\r\n";  
  6. $postStr .="Content-Type: image/png\r\n\r\n";  
  7. $postStr .=$uploadFile."\r\n";  
  8. $postStr .="--".$boundary."\r\n";//边界结束  
  9. fwrite($fp,"POST /a.php HTTP/1.0\r\n");  
  10. fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n");  
  11. fwrite($fp,"Content-length:".strlen($postStr)."\r\n\r\n");  
  12. fwrite($fp,$postStr);  
  13. while (!feof($fp)){  
  14.      echo fgets($fp, 128);  
  15. }  
  16. fclose($fp);  
  17.   
  18. print_r($_FILES);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值