谈一谈php://input和php://output

<p>对一php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述。</p><p>php://input 是个可以访问请求的原始数据的只读流。 POST 请求的情况下,最好使用 php://input 来代替 $HTTP_RAW_POST_DATA(原生的post数据),因为它不依赖于特定的 php.ini 指令,内存消耗更少<br/></p><p>“php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype=”multipart/form-data”.<br/></p><p>翻译过来,是这样:</p><p>“php://input可以读取没有处理过的POST数据。相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置。php://input不能用于enctype=multipart/form-data”<br/></p><p>我们应该怎么去理解这段概述呢?!我把它划分为三部分,逐步去理解。<br/></p><p>读取POST数据</p><p>不能用于multipart/form-data类型</p><p>php://input VS $HTTP_RAW_POST_DATA</p><p>PHPer们一定很熟悉$_POST这个内置变量。$_POST与 php://input存在哪些关联与区别呢?另外,客户端向服务端交互数据,最常用的方法除了POST之外,还有GET。既然php://input作 为PHP输入流,它能读取GET数据吗?</p><p>下面开始进行测试:</p><p>GET提交的表单:</p><p>index.php</p><p>&lt;form action=&quot;test.php&quot; method=&quot;get&quot;&gt; </p><p> &lt;input type=&quot;text&quot; name=&quot;user&quot;&gt; </p><p> &lt;input type=&quot;password&quot; name=&quot;password&quot;&gt; </p><p> &lt;input type=&quot;submit&quot;&gt; </p><p>&lt;/form&gt;</p><p>test.php</p><p>&lt;?php</p><p> header(&quot;Content-Type:text/html;charset=utf-8&quot;);</p><p> $file_in = file_get_contents(&quot;php://input&quot;);</p><p> echo $file_in;</p><p> echo &quot;&lt;br&gt;&quot;;</p><p> echo urldecode($file_in);</p><p>结果:</p><p><img src="http://www.outobe.com/up_pic/201907/021123318980.jpg"/> <br/></p><p><img src="/up_pic/201907/021125493704.jpg" title="021125493704.jpg" alt="2.jpg"/></p><p>POST提交表单:</p><p>index.php</p><p>&lt;form action=&quot;test.php&quot; method=&quot;post&quot;&gt; </p><p> &lt;input type=&quot;text&quot; name=&quot;user&quot;&gt; </p><p> &lt;input type=&quot;password&quot; name=&quot;password&quot;&gt; </p><p> &lt;input type=&quot;submit&quot;&gt; </p><p>&lt;/form&gt;</p><p>test.php</p><p>&lt;?php</p><p> header(&quot;Content-Type:text/html;charset=utf-8&quot;);</p><p> $file_in = file_get_contents(&quot;php://input&quot;);</p><p> echo $file_in;</p><p> echo &quot;&lt;br&gt;&quot;;</p><p> echo urldecode($file_in);</p><p>结果:</p><p><img src="/up_pic/201907/021126198940.jpg" title="021126198940.jpg" alt="1.jpg"/></p><p>说明:</p><p>1、GET提交时,不会指定Content-Type和Content-Length,它表示http请求body中的数据是使用http的post方法提交的表单数据,并且进行了urlencode()处理。</p><p> POST提交时,Content- Type取值为application/x-www-form-urlencoded时,也指明了Content-Length的值,php会将http请求body相应数据会填入到数 组$_POST,填入到$_POST数组中的数据是进行urldecode()解析的结果。</p><p>2,php://input数据,只要Content-Type不为 multipart/form-data(该条件限制稍后会介绍)。那么php://input数据与http entity body部分数据是一致的。该部分相一致的数据的长度由Content-Length指定。</p><p>3,仅当Content-Type为application/x-www-form-urlencoded且提交方法是POST方法时,$_POST数据与php://input数据才是”一致”(打上引号,表示它们格式不一致,内容一致)的。其它情况,它们都不一致。</p><p>4,php://input读取不到$_GET数据。是因为$_GET数据作为query_path写在http请求头部(header)的PATH字段,而不是写在http请求的body部分。</p><p>加了enctype=multipart/form-data的上传文件的时候</p><p>index.php</p><p>&lt;form enctype=&quot;multipart/form-data&quot; action=&quot;test.php&quot; method=&quot;post&quot;&gt; </p><p> &lt;input type=&quot;text&quot; name=&quot;user&quot;&gt; </p><p> &lt;input type=&quot;password&quot; name=&quot;password&quot;&gt; </p><p> &lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;&quot; /&gt;</p><p> &lt;input type=&quot;submit&quot;&gt; </p><p>&lt;/form&gt;</p><p>test.php</p><p>&lt;?php</p><p> header(&quot;Content-Type:text/html;charset=utf-8&quot;);</p><p> var_dump($_POST);</p><p> $file_in = file_get_contents(&quot;php://input&quot;);</p><p> echo $file_in;</p><p> echo &quot;&lt;br&gt;&quot;;</p><p> echo urldecode($file_in);</p><p>?&gt;</p><p>结果:</p><p><img src="/up_pic/201907/021126503286.jpg" title="021126503286.jpg" alt="1.jpg"/></p><p>说明:</p><p>从响应输出来比对,$_POST数据跟请求提交数据相符,即$_POST = array (size=2)</p><p> &#39;user&#39; =&gt; string &#39;PHP&#39; (length=3)</p><p> &#39;password&#39; =&gt; string &#39;100&#39; (length=3)。这也跟http请求body中的数据相呼应,同时说明PHP把相应的数据填入$_POST全局变量。而php://input 输出为空,没有输出任何东西,尽管http请求数据包中body不为空。这表示,当Content-Type为multipart/form-data的 时候,即便http请求body中存在数据,php://input也为空,PHP此时,不会把数据填入php://input流。所以,可以确定: php://input不能用于读取enctype=multipart/form-data数据。</p><p>我们会发现,最大不同的一 点是Content-Type后面跟了boundary定义了数据的分界符,bounday是随机生成的。另外一个大不一样的,就是http entity body中的数据组织结构不一样了。</p><p>们概述了,当Content-Type为application/x- www-form-urlencoded时,php://input和$_POST数据是“一致”的,为其它Content-Type的时候,php: //input和$_POST数据数据是不一致的。因为只有在Content-Type为application/x-www-form- urlencoded或者为multipart/form-data的时候,PHP才会将http请求数据包中的body相应部分数据填入$_POST全 局变量中,其它情况PHP都忽略。而php://input除了在数据类型为multipart/form-data之外为空外,其它情况都可能不为空。 通过这一节,我们更加明白了php://input与$_POST的区别与联系。所以,再次确认,php://input无法读取 enctype=multipart/form-data数据,当php://input遇到它时,永远为空,即便http entity body有数据。</p><p>相信大家对php://input已经有一定深度地了解了。那 么$http_raw_post_data是什么呢?$http_raw_post_data是PHP内置的一个全局变量。它用于,PHP在无法识别的 Content-Type的情况下,将POST过来的数据原样地填入变量$http_raw_post_data。它同样无法读取Content- Type为multipart/form-data的POST数据。需要设置php.ini中的 always_populate_raw_post_data值为On,PHP才会总把POST数据填入变量$http_raw_post_data。<br/></p><p>test.php</p><p>&lt;?php</p><p>$raw_post_data = file_get_contents(&#39;php://input&#39;, &#39;r&#39;);</p><p>$rtn = ($raw_post_data == $HTTP_RAW_POST_DATA) ? 1 : 0;</p><p>echo $rtn;</p><p>?&gt;</p><p>结果:</p><p>说明:</p><p>得出的结果输出都是一样的,即都为1,表示php://input和$HTTP_RAW_POST_DATA是相同的。至于对内存的压力,我们这里就不做细致地测试了。有兴趣的,可以通过xhprof进行测试和观察。</p><p>1, php://input 可以读取http entity body中指定长度的值,由Content-Length指定长度,不管是POST方式或者GET方法提交过来的数据。但是,一般GET方法提交数据 时,http request entity body部分都为空。<br/></p><p>2,php://input 与$HTTP_RAW_POST_DATA读取的数据是一样的,都只读取Content-Type不为multipart/form-data的数据。</p><p>php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.(摘自PHP官网)<br/></p><p>大概意思:php://output是一个只写流,允许您以与var_dump和echo相同的方式写入输出缓冲区机制。<br/></p><p>读取数据输出到浏览器(PHPExcel里的运用)<br/></p><p>&lt;?php</p><p>$dir=dirname(__FILE__);//查找当前脚本所在路径</p><p>require $dir.&quot;/db.php&quot;;//引入mysql操作类文件</p><p>require $dir.&quot;/PHPExcel/PHPExcel.php&quot;;//引入PHPExcel</p><p>$db=new db($phpexcel);//实例化db类 连接数据库,db.php引入了dbconfig.php</p><p>$objPHPExcel=new PHPExcel();//实例化PHPExcel类,等同于在桌面上新建一个excel</p><p>for($i=1;$i&lt;=3;$i++){</p><p>if($i&gt;1){</p><p>$objPHPExcel-&gt;createSheet();//再新建两张内置表</p><p>}</p><p>$objPHPExcel-&gt;setActiveSheetIndex($i-1);//把新创建的sheet设定为当前活动sheet</p><p>$objSheet=$objPHPExcel-&gt;getActiveSheet();//获取当前活动sheet</p><p>$objSheet-&gt;setTitle($i.&quot;年级&quot;);//给当前活动sheet起个名称</p><p>$data=$db-&gt;getDataByGrade($i);//查询每个年级的学生数据</p><p>$objSheet-&gt;setCellValue(&quot;A1&quot;,&quot;姓名&quot;)-&gt;setCellValue(&quot;B1&quot;,&quot;分数&quot;)-&gt;setCellValue(&quot;C1&quot;,&quot;班级&quot;);//填充数据</p><p>$j=2;//从第二行开始</p><p>foreach($data as $key=&gt;$val){</p><p>$objSheet-&gt;setCellValue(&quot;A&quot;.$j,$val[&#39;username&#39;])-&gt;setCellValue(&quot;B&quot;.$j,$val[&#39;score&#39;])-&gt;setCellValue(&quot;C&quot;.$j,$val[&#39;class&#39;].&quot;班&quot;);</p><p>$j++;</p><p>}</p><p>}</p><p>$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,&#39;Excel5&#39;);//生成2003版本的excel文件</p><p>browser_export(&#39;Excel5&#39;,&#39;browser_excel03.xls&#39;);//输出到浏览器</p><p>$objWriter-&gt;save(&quot;php://output&quot;);</p><p>function browser_export($type,$filename){</p><p>if($type==&quot;Excel5&quot;){</p><p>header(&#39;Content-Type: application/vnd.ms-excel&#39;);//告诉浏览器将要输出excel03文件</p><p>}else{</p><p>header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);//告诉浏览器数据excel07文件</p><p>}</p><p>header(&#39;Content-Disposition: attachment;filename=&quot;&#39;.$filename.&#39;&quot;&#39;);//告诉浏览器将输出文件的名称</p><p>header(&#39;Cache-Control: max-age=0&#39;);//禁止缓存</p><p>}</p><p>?&gt;</p><p>总结:</p><p>1,Coentent-Type仅在取值为application/x-www-data-urlencoded和multipart/form-data两种情况下,PHP才会将http请求数据包中相应的数据填入全局变量$_POST</p><p>2,PHP不能识别的Content-Type类型的时候,会将http请求包中相应的数据填入变量$HTTP_RAW_POST_DATA</p><p>3, 只有Coentent-Type不为multipart/form-data的时候,PHP不会将http请求数据包中的相应数据填入php://input,否则其它情况都会。填入的长度,由Coentent-Length指定。</p><p>4,只有Content-Type为application/x-www-data-urlencoded时,php://input数据才跟$_POST数据相一致。</p><p>5,php://input数据总是跟$HTTP_RAW_POST_DATA相同,但是php://input比$HTTP_RAW_POST_DATA更凑效,且不需要特殊设置php.ini</p><p>6,PHP会将PATH字段的query_path部分,填入全局变量$_GET。通常情况下,GET方法提交的http请求,body为空。</p>
返回顶部 留言