php http请求内容原始数据enctype = multipart / form-data [英] php http request content raw data enctype=multipart/form-data

查看:235
本文介绍了php http请求内容原始数据enctype = multipart / form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写两个类:HttpRequest和HttpResponse。
我想编写自己的HTTP类。



使用enctype = multipart / form-data格式的POST方法遇到问题:经过长时间的研究和搜索,我发现我必须使用 file_get_contents(php: //输入)来获取请求内容。
当我测试它时,我有一个空字符串 var_dump(file_get_contents(php:// input))



我无法访问server / php配置。



我使用以下代码进行测试:

 <?php 
$ input = file_get_contents('php:// input');
var_dump($ input);
?>
< html>
< body>
< form action =./method =postenctype =multipart / form-data>
< label for =file>档案名称:< / label>
< input type =textname =namenid =nameid/>< br />
< input type =filename =fileid =file>< br>
< input type =filename =file2id =file2>< br>
< input type =submitname =submitvalue =Submit>
< / form>
< / body>
< / html>

提交后,我得到如下结果: string(0)
这很明显,因为php://输入不适用于enctype =multipart / form-data表单。



我想要一些东西例如:

  ------------------------ ----- 19972703421793859182225487886 
Content-Disposition:form-data; name =namen

ds
----------------------------- 19972703421793859182225487886
Content-Disposition:form-data; NAME = 文件; filename =toto.xml
Content-Type:text / xml

<?xml version =1.0encoding =UTF-8?>
<多多>< /多多>

----------------------------- 19972703421793859182225487886
Content-Disposition:form-data; NAME = file2的; filename =
Content-Type:application / octet-stream


--------------------- -------- 19972703421793859182225487886
内容处理:form-data; name =submit

提交
----------------------------- 19972703421793859182225487886--

是否适用于任何请求方法?或者我错了?

解决方案

我在这里找到一个明显的问题:为什么还要努力去解析它呢?通过使用$ _POST [] / $ _FILES [] ..

,您可以轻松使用数据。您仍然可以围绕这些值构建自己的类,而不是你自己的类尝试完成与php编译器已经为你做的相同的数据数组。



为了给你一个有效的答案:我不知道一种获取你的php://输入的方法,因为它可能被PHP编译器读取,因此你在流的末尾(加上:它是只读的,因此可能无法将指针重置为流的开始)。

I'm currently coding two classes: HttpRequest and HttpResponse. I want to code my own HTTP classes.

I'm encountering an issue with a POST method using a form with enctype=multipart/form-data: I can't get the request contents.

After a long research and searchs, I've found I have to use file_get_contents("php://input") to get the request content. When I test it, I have an empty string var_dump(file_get_contents("php://input")).

I have no access to server/php configuration.

I'm testing with the following code :

<?php
$input = file_get_contents('php://input');
var_dump($input);
?>
<html>
<body>
<form action="./" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="text" name="namen" id="nameid" /><br/>
    <input type="file" name="file" id="file"><br>
    <input type="file" name="file2" id="file2"><br>
    <input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

And after submitting, I have this result : string(0) "". It's obvious because php://input doesn't work with enctype="multipart/form-data" forms.

I'd like to have something like :

-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="namen"

ds
-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="file"; filename="toto.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<Toto></Toto>

-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="file2"; filename=""
Content-Type: application/octet-stream


-----------------------------19972703421793859182225487886
Content-Disposition: form-data; name="submit"

Submit
-----------------------------19972703421793859182225487886--

Is a way it works for any request method ? Or am I wrong ?

解决方案

I'm going for the obvious one here: why even bother trying to parse this yourself? The data is within your range with the easy use of $_POST[] / $_FILES[] ..

You can still build your own class around those values instead of having your own classes try to accomplish the same data array as the php-compiler already did for you.

In order to give you a valid answer: I don't know of a way to get your php://input back, as it is probably read by the PHP compiler and therefore you are at the end of the stream ( plus: it is read-only so there is probably no way to reset the pointer to the start of the stream ).

这篇关于php http请求内容原始数据enctype = multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆