沃梦达 / 编程问答 / php问题 / 正文

php http 请求内容原始数据 enctype=multipart/form-data

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

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

问题描述

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

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

我在使用带有 enctype=multipart/form-data 的表单的 POST 方法时遇到问题:我无法获取请求内容.

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

经过长时间的研究和搜索,我发现我必须使用 file_get_contents("php://input") 来获取请求内容.当我测试它时,我有一个空字符串 var_dump(file_get_contents("php://input")).

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")).

我无法访问服务器/php 配置.

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>

提交后,我得到了这个结果:string(0) "".很明显,因为 php://input 不适用于 enctype="multipart/form-data" 表单.

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

我想要类似的东西:

-----------------------------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 ?

推荐答案

我要在这里寻找一个显而易见的:为什么还要费心去尝试自己解析这个?数据在您的范围内,易于使用 $_POST[]/$_FILES[] ..

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[] ..

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

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.

为了给你一个有效的答案:我不知道有什么方法可以让你的 php://input 回来,因为它可能被 PHP 编译器读取,因此你在流的末尾(另外:它是只读的,因此可能无法将指针重置为流的开头).

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php http 请求内容原始数据 enctype=multipart/form-data

基础教程推荐