php 模拟post上传文件

脚本:a.php

1 $stream = file_get_contents('1.jpg');
2  
3 $len = strlen($stream);

第一种方法用sock方法连接模拟POST提交,b.php虽然获取了post传送过来的二进制数据,但是与原文件的md5值,并且文件也打不开。

//$fp = fsockopen("127.0.0.1",80);
$d = '';

/*if ($fp) {
fputs($fp, "POST /test/b1.php HTTP/1.1\r\n");
fputs($fp, "Host: localhost\r\n");
fputs($fp, "Content-Type: text/html\r\n");
fputs($fp, "Content-Length: {$len}\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp,  "n=".$stream. "\r\n\r\n");

while (!feof($fp)) {
$d .= fgets($fp, 4096);
}
fclose($fp);
echo $d;
}*/

第二种方法用cur模拟post提交数据,这种方法可以得到传送过来的post数据,并与原文件的md5值相同.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/test/b1.php");
curl_setopt($ch, CURLOPT_POST, 1);//启用POST提交
curl_setopt($ch, CURLOPT_PORT, 80); //设置端口
curl_setopt($ch,CURLOPT_POSTFIELDS,$stream);
curl_exec($ch);
curl_close($ch);

第三种方法,用curl模拟表单上传文件

$data = array(
"file2"  => "@".realpath("1.jpg")
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/test/b1.php");
curl_setopt($ch, CURLOPT_POST, 1);//启用POST提交
curl_setopt($ch, CURLOPT_PORT, 80); //设置端口
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$aa = curl_exec($ch);
curl_close($ch);

脚本:b.php

$str = file_get_contents("php://input");

file_put_contents("2.jpg",$str);

转载于:https://www.cnblogs.com/kudosharry/articles/2506286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值