php提交表单access,浅谈php表单提交中enctype属性

实际项目中,form表单的enctype属性设置成了“text/plain”,$_POST[]取值取不到。解决方法是删除该属性,或者设置为其他的另外两种属性值。遇到的问题,记录在此。

这里再来熟悉一下php中form表单中enctype属性的设置。

form表单中的enctype属性指定将数据发回到服务器时浏览器使用的编码类型。

下面是取值说明:

multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,不对字符编码。当使用有文件上传控件的表单时,该值是必需的。。

application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。在发送前对所有字符进行编码(默认)。

text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符,邮件处理中用到,其他地方很少用。将空格转换为 "+" 符号,但不编码特殊字符。抓包可见数据形式。

说明:

1)如果表单中有文件要上传,表单中form标签必须设置enctype="multipart/form-data"来确保匿名上传文件的MIME编码。默认情况下,表单的编码格式是 application/x-www-form-urlencoded,不能用于文件上传;

2)进行session处理时,如果将表单设置enctype="text/plain",则通过$_POST,$_GET,$_REQUEST等是无法取到值的。这点一定要小心!!!

对于三种类型的进一步解释,实质参考如下:

enctype defines how the data should be formatted before sending. the two correct formats are application/x-www-form-urlencoded (which essentially sends things as key=valye&anotherkey=anothervalue, with http headers) and multipart/form-data (which splits each key up into a distinct section of data). text/plain means nothing should be done - its behaviour is essentially undefined between browsers, and was only ever used for automated email forms in the days before spam. use application/x-www-form-urlencoded for text forms (or leave this to use the default setting) and multipart/form-data for file attachment uploads

text/plain:Content-Type: text/plain  ================================================================================  foo=bar

baz=The first line.  The second line.

application/x-www-form-urlencoded:

Content-Type: application/x-www-form-urlencoded  ================================================================================

foo=bar&baz=The+first+line.%0D%0AThe+second+line.%0D%0A

multipart/form-data:

Content-Type: multipart/form-data; boundary=---------------------------314911788813839

================================================================================

-----------------------------314911788813839  Content-Disposition: form-data; name="foo"

bar  -----------------------------314911788813839  Content-Disposition: form-data; name="baz"

The first line.  The second line.    -----------------------------314911788813839--

you can see how the first one is useful for passing values UNCHECKED directly into an email program (e.g. if your form uses a mailto: address, so it uses your visitor's email program to send) but is largely useless for programmatic access to fields. only the bottom 2 examples are valid for actual form-based form data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用华为云OBS技术上传图片文件的示例代码,在前端使用HTML和JavaScript实现,后端使用Java: 前端代码: ```html <form enctype="multipart/form-data"> <input type="text" name="name" placeholder="请输入名称"> <input type="file" name="file"> <button type="button" onclick="uploadFile()">提交</button> </form> <script> function uploadFile() { var name = document.querySelector('input[name="name"]').value; var file = document.querySelector('input[name="file"]').files[0]; var formData = new FormData(); formData.append("name", name); formData.append("file", file); var xhr = new XMLHttpRequest(); xhr.open("POST", "/upload"); xhr.send(formData); } </script> ``` 在这个示例,我们使用HTML表单和JavaScript来获取用户输入的名称和图片文件,然后使用`FormData`对象将其打包为一个HTTP请求的主体,并使用XMLHttpRequest对象发送POST请求到后端的`/upload`接口。 后端代码: ```java // 引入OBS Java SDK import com.obs.services.ObsClient; import com.obs.services.model.PutObjectResult; // 处理上传请求 @PostMapping("/upload") public String handleUpload(HttpServletRequest request) throws IOException { String name = request.getParameter("name"); Part filePart = request.getPart("file"); // 初始化OBS客户端 ObsClient obsClient = new ObsClient("<access_key_id>", "<secret_access_key>", "<endpoint>"); // 上传文件 String bucketName = "<bucket_name>"; String objectKey = name + "_" + System.currentTimeMillis() + ".jpg"; InputStream inputStream = filePart.getInputStream(); PutObjectResult putObjectResult = obsClient.putObject(bucketName, objectKey, inputStream); // 打印上传结果 System.out.println("Upload success. ETag: " + putObjectResult.getEtag()); // 关闭OBS客户端 obsClient.close(); return "success"; } ``` 在这个示例,我们使用Java的Spring框架来接收前端发送的POST请求,并从请求参数获取名称和文件。然后,我们使用OBS Java SDK来初始化OBS客户端,并将文件上传到我们指定的华为云OBS桶。最后,我们打印上传结果并关闭OBS客户端。 需要注意的是,示例的`<access_key_id>`、`<secret_access_key>`和`<endpoint>`需要替换为你的华为云OBS账户的访问密钥ID、访问密钥和OBS服务的Endpoint。`<bucket_name>`也需要替换为你要上传文件的桶名。此外,示例只上传了图片文件,如果需要上传其他类型的文件,需要修改代码的文件名后缀和文件类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值