php://input和$_post的区别是什么?下面本篇文章给大家介绍一下。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

手册中摘取的几句话:
当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data 时,会将变量以关联数组形式传入当前脚本。php://input 是个可以访问请求的原始数据的只读流。 enctype="multipart/form-data" 的时候php://input 是无效的。
验证下:
post.html
立即学习“PHP免费学习笔记(深入)”;
getpost.php
";
var_dump(file_get_contents('php://input', 'r'));
echo "----------post---------
";
var_dump($_POST);
?>一、enctype="application/x-www-form-urlencoded"
请求主体:
Content-Type: application/x-www-form-urlencoded Content-Length: 25name=saisai&submit=submit
输出:
----------input-------- string 'name=saisai&submit=submit' (length=25) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小结:当enctype="application/x-www-form-urlencoded"时,请求主体(request body)中的数据(name=saisai&submit=submit)转换成关联数组放入$_POST,而 php://input 则获取的是原始数据(raw data)。
二、enctype=“multipart/form-data”时
2.1 表单:
请求主题:
Content-Type: multipart/form-data; boundary=---------------------------22554656810024 Content-Length: 249 -----------------------------22554656810024 Content-Disposition: form-data; name="name" saisai -----------------------------22554656810024 Content-Disposition: form-data; name="submit" submit -----------------------------22554656810024--
输出:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小结:在enctype="multipart/form-data" 且没有上传文件控件时,$_POST 能正常打印数据,php:// 无效。
牛NIUCMS本地O2O系统是一个以php+mysql进行开发的o2o网站系统。NIUCMS是一款强大的网站管理系统。支持智慧城市、智慧小区、智慧乡村、本地生活门户、本地O2O平台的构建。请注意以下几点:1、这套源码必须要服务器支持伪静态,是支持.htaccess规则的伪静态,一般Apache服务器支持,别搞的下载回去以后说什么缺 少文件,其实源码并非缺少文件。2、这套源码请在php 5.4环境下
2.2 表单(添加一个文件上传):
请求主题:
Content-Type: multipart/form-data; boundary=---------------------------272321281228527
Content-Length: 68386
-----------------------------272321281228527
Content-Disposition: form-data; name="name"
saisai
-----------------------------272321281228527
Content-Disposition: form-data; name="filename"; filename="dog.png"
Content-Type: image/png
一堆乱码
-----------------------------272321281228527
Content-Disposition: form-data; name="submit"
submit
-----------------------------272321281228527--输出:
----------input-------- string '' (length=0) ----------post--------- array (size=2) 'name' => string 'saisai' (length=6) 'submit' => string 'submit' (length=6)
小结:在enctype="multipart/form-data" 且有上传文件控件时,$_POST 能打印出传入的数据,但是排除了上传的任何内容。php:// 无效。
三、enctype="text/plain"
表单:
请求主体:
Content-Type: text/plain Content-Length: 28 name=saisai submit=submit
输出:
----------input-------- string 'name=saisai submit=submit ' (length=28) ----------post--------- array (size=0) empty
小结:enctype="text/plain"时,$_POST中没有内容,php://input中以键值对的方式存放。
总结:
当 HTTP POST 请求的 Content-Type 是 application/x-www-form-urlencoded 或 multipart/form-data :php://input 中是形同 a=1&b=2的原始数据。$_POST 中是关联数组,且没有上传控件的内容。
php://input 是个可以访问请求的原始数据的只读流。 enctype="multipart/form-data" 的时候php://input 是无效的。
$_POST 不能获取 Content-Type = "text/plain"时 post的数据, php://input可以。
更多相关知识,请关注 PHP中文网!!










