2023年6月27日发(作者:)
为什么上传⽂件的表单需要设置enctype=multipartform-data在学习PHP⽂件上传的过程中发现,HTML表单需要设置enctype="multipart/form-data"这个属性,虽然不这么设置的确⽆法上传,但这是为什么呢?HTML表单如何打包数据⽂件是由enctype这个属性决定的。enctype有以下⼏种取值:application/x-www-form-urlencoded在发送前编码所有字符(默认)(空格被编码为’+’,特殊字符被编码为ASCII⼗六进制字符)multipart/form-data 不对字符编码。在使⽤包含⽂件上传控件的表单时,必须使⽤该值。text/plain 空格转换为 “+” 加号,但不对特殊字符编码。默认enctype=application/x-www-form-urlencoded,所以表单的内容会按URL规则编码,然后根据表单的提交⽅法:method=’get’ 编码后的表单内容附加在请求连接后method=’post’ 编码后的表单内容作为post请求的正⽂内容我们通过抓包软件来分析⼀下这⼏种⽅式产⽣的请求的差别实验⼀条件method='get'enctype=application/x-www-form-urlencoded对应的html代码为:
⽂本框中输⼊"hello world",选择⽂件,点击提交,浏览器发出的HTML包为:GET /xxx?name=%22hello+world%22&file=&submit=submit HTTP/1.1Host: nection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36Referer: /cept-Encoding: gzip, deflate, sdchAccept-Language: zh-CN,zh;q=0.8,en;q=0.6因为是get请求,所以只有头部没有正⽂。请求的链接为/xxx?name=hello+world.&file=&submit=submit,可以看到表单的信息已经被编码到URL中了。注意两点:1."hello world"被编码为%22hello+world%22,特殊字符和空格都被编码2.
type='file'提交的⽂件内容并没有被提交,只是把⽂件名编码到了URL中实验⼆条件method='post'enctype=application/x-www-form-urlencoded对应的html代码为:
⽂本框中输⼊"hello world",选择⽂件,点击提交,浏览器发出的HTML包为:POST /xxx HTTP/1.1Host: nection: keep-aliveContent-Length: 50Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: rade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36Content-Type: application/x-www-form-urlencodedReferer: /cept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8,en;q=0.6name=%22hello+world%22&file=&submit=submit与get请求相⽐,只是把name=hello+world.&file=&submit=submit放在了正⽂中,其他没有区别了注意两点:1."hello world"被编码为%22hello+world%22,特殊字符和空格都被编码2.
type='file'提交的⽂件内容并没有被提交,只是把⽂件名编码到了正⽂中实验三条件method='get'enctype='multipart/form-data'对应的html代码为:
⽂本框中输⼊"hello world",选择⽂件,点击提交,浏览器发出的HTML包为:GET /xxx?name=%22hello+world%22&file=&submit=submit HTTP/1.1Host: nection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36Referer: /cept-Encoding: gzip, deflate, sdchAccept-Language: zh-CN,zh;q=0.8,en;q=0.6结果和实验⼀⼀模⼀样,说明get和multipart/form-data配合⽆效。实验四条件method='post'enctype=multipart/form-data对应的html代码为:⽂本框中输⼊"hello world",选择⽂件,点击提交,浏览器发出的HTML包为:POST /xxx HTTP/1.1Host: nection: keep-aliveContent-Length: 3695Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: rade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryIZDrYHwuf2VJdpHwReferer: /cept-Encoding: gzip, deflateAccept-Language: zh-CN,zh;q=0.8,en;q=0.6------WebKitFormBoundaryIZDrYHwuf2VJdpHwContent-Disposition: form-data; name="name""hello world"------WebKitFormBoundaryIZDrYHwuf2VJdpHwContent-Disposition: form-data; name="file"; filename=""Content-Type: image/....------WebKitFormBoundaryIZDrYHwuf2VJdpHwContent-Disposition: form-data; name="Y../..,+|.$G?...P.P,,...m..v.7. %...%.IR$....|IDAT(.:.?.......}.(.Pd`A..V...L...?..#.....4.o..LS.....W.d.?...A8..LS...(.u.......D这次与前两次相⽐有很⼤的不同。请求主题的内容复杂很多。根据boundary定义的字符串,正⽂被分割为⼏个部分,每个部分与表单中的内容⼀⼀对应。每部分内容,还会由Content-Disposition: form-data; name="name"这样的字符串指定内容,与名字。对于⽂件内容,还有有额外的两个字段filename=""‘和Content-Type: image/png,并且⽂件的内容就直接附加在后⾯。总结所以,只有使⽤enctype="multipart/form-data",表单才会把⽂件的内容编码到HTML请求中。参考⽂章发布者:admin,转转请注明出处:http://www.yc00.com/web/1687864974a51968.html
评论列表(0条)