ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
>[danger] 先来看最基本的文件上传表单,以及服务器接收到的数据结构 a.php ~~~ <?php if ($_POST) { echo '<pre>'; print_r($_POST); echo '</pre>'; echo '<hr>'; echo '<pre>'; print_r($_FILES); echo '</pre>'; exit; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="keywords" content="keywords"> <meta name="description" content="description"> <link href="favicon.ico" rel="icon" type="image/x-icon"> <link href="" type="text/css" rel="stylesheet"> <script src=""></script> <title>Document</title> </head> <body> <form action="" method="post" enctype="multipart/form-data"> <input type="text" name="val" value="val"><br /> <input type="file" name="logo"><br /> <input type="file" name="pic"><br /> <input type="file" name="list[]"><br /> <input type="file" name="list[]"><br /> <input type="file" name="img-list[]"><br /> <input type="file" name="img-list[]"><br /><br /> <!-- 如果有name有值,则就会有一个$_POST[s] = 提交的值 --> <input type="submit" name="s" value="提交"><br /> <!-- 没有提交效果 --> <input type="button" name="b" value="提交按钮"><br /> <!-- 在form中有提交效果,type="submit" 也有提交效果,但是 type="button" 就没有提交效果了 (注意所有的提交按钮必须在form中才能提交除非用js控制) --> <button>按钮</button> </form> </body> </html> ~~~ ![](https://box.kancloud.cn/992ce43b78c6c6e30255b27d67dc0714_314x280.jpg) 点击 提交 : ~~~ Array ( [val] => val [s] => 提交 ) Array ( [logo] => Array ( [name] => QQ图片20161106223133.jpg [type] => image/jpeg [tmp_name] => D:\wamp64\tmp\php92D0.tmp [error] => 0 [size] => 217991 ) [pic] => Array ( [name] => QQ图片20161106223133.jpg [type] => image/jpeg [tmp_name] => D:\wamp64\tmp\php931F.tmp [error] => 0 [size] => 217991 ) [list] => Array ( [name] => Array ( [0] => QQ图片20161106223133.jpg [1] => fdc20716fe1372b0859ec0019d7e6194.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg ) [tmp_name] => Array ( [0] => D:\wamp64\tmp\php932F.tmp [1] => D:\wamp64\tmp\php9350.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 217991 [1] => 6768 ) ) [img-list] => Array ( [name] => Array ( [0] => QQ图片20161106223133.jpg [1] => bg-01.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg ) [tmp_name] => Array ( [0] => D:\wamp64\tmp\php9351.tmp [1] => D:\wamp64\tmp\php9361.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 217991 [1] => 471588 ) ) ) ~~~ **上面就是所有最基本的表单上传,最基本的上传数据结构了。** 文件上传根据类型,可分为两种: - **单文件上传** - 单一字段 - **多文件上传** - 多字段的形式多文件 - 单一字段数组(img[])的形式多文件上传**(这种最复杂,注意这种形式的数据结构)** - 组合多文件(上面几种情况的组合) 参考: [修复Request->file()判断临时文件不严谨导致多文件上传失败的情况](https://github.com/top-think/framework/pull/348) [文件上传那些事儿](https://mp.weixin.qq.com/s/ni_5rZDCOypxtHrVXq1Aww) ### 扩展 ~~~html <input type="file" multiple name="pic[]"> ~~~ `multiple`是HTML5新增的属性,这个属性可以使上传表单上传时选择多个文件,点击“选择文件”,在弹出系统选择文件窗口中按住`Ctrl`键可以连续选择多个文件,**但是此属性必须配合pic[]数组字段的形式才有作用(注意有些上传插件比如uploadify,这些可以选择多文件上传,此时可能也有这个问题,注意检查上传字段名的设置)**,否则即使选择多个文件,提交到服务器也只有最后一个选择的文件。(所以这个属性可以用来避免以前上传多文件需要多表单,仅一个表单就搞定了) * * * * * ### 其他 对于前端来说,图片上传有以下知识点: - 普通上传 - ajax无刷新文件上传 - 大文件分块上传 - MD5提前校验去重,存在不上传(传到服务端再校验用处不大,只是节省空间,但是并没有节省不必要上传的流量) - 图像压缩上传 - 图像裁剪(放大,缩小,旋转等手势操作) 这些操作以前只有flash才能全部做到,现在html5的新API也能够做到了,不过大部分接口对浏览器的版本要求比较高。 服务端收到文件的方式无非两种: 1. 普通的$\_FIELS 2. base64编码的字符图片$\_POST(非常规Coentent-Type时用`file_get_contents('php://input', 'r')`) **参考:** [php://input - 简单--生活 - 博客园](http://www.cnblogs.com/xiangxiaodong/archive/2012/11/07/2758685.html) [PHP: php:// - Manual](http://php.net/manual/zh/wrappers.php.php) * * * * * [Http 历险记(上)](https://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665513069&idx=1&sn=548c497c46c7c076145064a120c7c101&scene=21#wechat_redirect) [Http历险记(下)-- Struts的秘密](https://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665513080&idx=1&sn=d24a4cdfc71412c581393d584fd91326&scene=21#wechat_redirect) * * * * * last update:2017-8-17 09:45:38