http协议传输二进制数据以及对输入流(php://input)和http请求的理解

本文介绍了使用PHP进行文件上传及通过HTTP POST方式传输图片文件的过程。具体包括使用file_get_contents读取本地文件内容,设置HTTP请求头进行POST请求,并通过stream_context_create创建流上下文。同时展示了如何在接收端通过php://input获取POST请求中的文件数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.index.php

<?php

$data=file_get_contents('./a.jpg');
$opts = array('http' =>
        array(
                'method'  => 'POST',
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n"
                ."Content-Length: " . strlen($data) . "\r\n",
                //    "Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
                'content' => $data,
                'timeout' => 60
        )
);

$context  = stream_context_create($opts);
$url = 'http://127.0.0.1/testhttpstream/stream.php';
$result = file_get_contents($url, false, $context);
header('Content-type:image/jpg');
echo $result;
?>
View Code

2.stream.php

<?php
$data=file_get_contents('php://input');
echo $data;

3.http请求(除get请求)里也有正文,而不是只有请求头而已,php://input获取的就是请求里的正文.

转载于:https://www.cnblogs.com/zuoxiaobing/p/3672942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值