方法1:

在html表单,放置多个文件选择框, 使用数组名作为组件的名字,如下:

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile[]"/>
<input type="file" name="upfile[]"/>
<input type="file" name="upfile_2[]"/>
<input type="file" name="upfile_2[]"/>
</form>

在服务器端我们可以测试一下提交的信息

<?php
print_r($_FILES);
?>

输出结果:

Array
(
[upfile] => Array
(
[name] => Array
(
[0] => C函数速查.chm
[1] => JDK_API_1_6中文帮助.CHM
) [type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
) [tmp_name] => Array
(
[0] => D:\PHP\xampp2\tmp\phpF7E1.tmp
[1] => D:\PHP\xampp2\tmp\phpF7E2.tmp
) [error] => Array
(
[0] => 0
[1] => 0
) [size] => Array
(
[0] => 98791
[1] => 36830335
) ) [upfile_2] => Array
(
[name] => Array
(
[0] => jquery1.7.2中文手册.chm
[1] => jQuery1.8.3中文手册.chm
) [type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
) [tmp_name] => Array
(
[0] => D:\PHP\xampp2\tmp\phpF93A.tmp
[1] => D:\PHP\xampp2\tmp\phpF93B.tmp
) [error] => Array
(
[0] => 0
[1] => 0
) [size] => Array
(
[0] => 306357
[1] => 405941
) ) )

方法2:

在html端为每一个input框给一个不同的name

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile_1"/>
<input type="file" name="upfile_2"/>
<input type="file" name="upfile_3"/>
</form>

服务端“print_r($_FILES);” 后,输出的信息:

Array
(
[upfile_1] => Array
(
[name] => C函数速查.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\php2247.tmp
[error] => 0
[size] => 98791
) [upfile_2] => Array
(
[name] => JDK_API_1_6中文帮助.CHM
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\php2248.tmp
[error] => 0
[size] => 36830335
) [upfile_3] => Array
(
[name] => jquery1.7.2中文手册.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\php23B0.tmp
[error] => 0
[size] => 306357
) )

所以,针对下面这个“综合性”上传表单:

<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="upfile[]"/>
<input type="file" name="upfile[]"/>
<input type="file" name="upfile_2[]"/>
<input type="file" name="upfile_2[]"/>
<input type="file" name="upfile_3"/>
<input type="file" name="upfile_4"/>
</form>

服务端接收到的数据为:

Array
(
[upfile] => Array
(
[name] => Array
(
[0] => C函数速查.chm
[1] => JDK_API_1_6中文帮助.CHM
) [type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
) [tmp_name] => Array
(
[0] => D:\PHP\xampp2\tmp\php4440.tmp
[1] => D:\PHP\xampp2\tmp\php4441.tmp
) [error] => Array
(
[0] => 0
[1] => 0
) [size] => Array
(
[0] => 98791
[1] => 36830335
) ) [upfile_2] => Array
(
[name] => Array
(
[0] => jquery1.7.2中文手册.chm
[1] => jQuery1.8.3中文手册.chm
) [type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
) [tmp_name] => Array
(
[0] => D:\PHP\xampp2\tmp\php459A.tmp
[1] => D:\PHP\xampp2\tmp\php459B.tmp
) [error] => Array
(
[0] => 0
[1] => 0
) [size] => Array
(
[0] => 306357
[1] => 405941
) ) [upfile_3] => Array
(
[name] => php_manual_zh.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\php459C.tmp
[error] => 0
[size] => 31019182
) [upfile_4] => Array
(
[name] => TIPI深入理解PHP内核_2014-04-29_V0.8.3.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\php4687.tmp
[error] => 0
[size] => 1304181
) )

问题:上面的 $_FILES 信息有点乱,可以写个函数/方法来格式化下,参考代码如下:

function format_files($files)
{
$fileArray = array();
$n = 0;
foreach ($files as $key => $file)
{
if (is_array($file['name']))
{
$keys = array_keys($file);
$count = count($file['name']);
for ($i = 0; $i < $count; $i++)
{
$fileArray[$n]['key'] = $key;
foreach ($keys as $_key)
{
$fileArray[$n][$_key] = $file[$_key][$i];
}
$n++;
}
}
else
{
$fileArray[$n] = $file;
$fileArray[$n]['key'] = $key;
$n++;
}
} return $fileArray;
}

经过 format_files($_FILES); 处理后,结果被格式化为:

Array
(
[0] => Array
(
[key] => upfile
[name] => C函数速查.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF27F.tmp
[error] => 0
[size] => 98791
) [1] => Array
(
[key] => upfile
[name] => JDK_API_1_6中文帮助.CHM
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF280.tmp
[error] => 0
[size] => 36830335
) [2] => Array
(
[key] => upfile_2
[name] => jquery1.7.2中文手册.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF3C9.tmp
[error] => 0
[size] => 306357
) [3] => Array
(
[key] => upfile_2
[name] => jQuery1.8.3中文手册.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF3CA.tmp
[error] => 0
[size] => 405941
) [4] => Array
(
[name] => php_manual_zh.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF3CB.tmp
[error] => 0
[size] => 31019182
[key] => upfile_3
) [5] => Array
(
[name] => TIPI深入理解PHP内核_2014-04-29_V0.8.3.chm
[type] => application/octet-stream
[tmp_name] => D:\PHP\xampp2\tmp\phpF4C6.tmp
[error] => 0
[size] => 1304181
[key] => upfile_4
) )

延伸阅读:

PHP上传(单个)文件示例

PHP同时上传“多个”文件示例,并格式化$_FILES数组信息的更多相关文章

  1. php多文件上传类(含示例)

    在网上看到一个比较好的多文件上传类,自己改良了下,顺便用js实现了多文件浏览,php文件上传原理都是相同的,多文件上传也只是进行了循环上传而已,当然你也可以使用swfupload进行多文件上传! &l ...

  2. thinkphp如何一次性的上传多个文件,在文件域中可以多选?

    可以做到类似于某度网盘的样式吗? 文件夹的命名, 可以用单数, 也可以用复数, 在同一个项目中, 只要统一就好了. 毕竟项目开发不同于英语写作. 建议使用缩写, 不管是不是缩写都用单数, 这样简洁,容 ...

  3. [转]ExtJs入门之filefield:文件上传的配置+结合Ajax完美实现文件上传的asp.net示例

    原文地址:http://www.stepday.com/topic/?459 作文一个ExtJs的入门汉子,学习起来的确是比较费劲的事情,不过如今在这样一个网络资源如此丰富的时代,依然不是那么难了的. ...

  4. 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!

    利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...

  5. 使用PHP实现文件上传和多文件上传

    PHP 2013 年 9 月 4 日 暂无评论 在PHP程序开发中,文件上传是一个使用非常普遍的功能,也是PHP程序员的必备技能之一.值得高兴的是,在PHP中实现文件上传功能要比在Java.C#等语言 ...

  6. flask 文件上传(单文件上传、多文件上传)

    文件上传 在HTML中,渲染一个文件上传字段只需要将<input>标签的type属性设为file,即<input type=”file”>. 这会在浏览器中渲染成一个文件上传字 ...

  7. 【Loadrunner】使用LoadRunner上传及下载文件

    使用LoadRunner上传及下载文件 1)LoadRunner上传文件 web_submit_data("importStudent.do", "Action=http ...

  8. C# HTTP系列13 以form-data方式上传多个文件以及键值对集合到远程服务器

    系列目录     [已更新最新开发文章,点击查看详细] 类似于以下场景,将表单中的用户信息(包含附件)上传到服务器并保存到数据库中, <form id="form1" run ...

  9. Spring MVC-------文件上传,单文件,多文件,文件下载

    Spring MVC 框架的文件上传是基于 commons-fileupload 组件的文件上传,只不过 Spring MVC 框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现,取消了 ...

随机推荐

  1. NopCommerce Alipay 支付插件

    NopCommerce Alipay 支付插件 1.查找及下载NopCommerce Alipay插件 http://www.nopcommerce.com/p/963/alipay-payment- ...

  2. 【2016-11-1】【坚持学习】【Day16】【MongoDB】【复制集 分片】

    Mongodb 两种集群方式 复制集 通常是一主一从,一主多从 mongodb的复制至少需要两个节点.其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据. mongodb各 ...

  3. Flash Builder 4.7 注册机完美激活方法

    Flash Builder 4.7 破解注册方法目前较为通用的三种方法: 第一种:三步修改配置文件方法,这种有开发者反应这种方法在升级和创建纯 AS 项目时会存在问题,但我懒的去试这种方法的主要原因是 ...

  4. [No000087]Linq排序,SortedList排序,二分法排序性能比较

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  5. TEXTAREA自适应文字行数的多少

    <textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight&qu ...

  6. Computer vision labs

    积累记录一些视觉实验室,方便查找 1.  多伦多大学计算机科学系 2.  普林斯顿大学计算机视觉和机器人实验室 3.  牛津大学Torr Vision Group 4.  伯克利视觉和学习中心 Pro ...

  7. 【腾讯GAD暑期训练营游戏程序班】游戏中的物理系统作业说明文档

    一.需求分析• 添加一辆新NPC车,可以让其与主角车碰撞:• 添加一些新物件,能够与车互动,在其触发事件将其移除:• 添加一些无法撞动的事件:• 添加NPC车的自动移动逻辑:• 在课上赛车的示例上添加 ...

  8. apt-get 相關設定

    /etc/apt/apt.conf.d/01proxy 若加了以下這行,則 apt-get 都會透過下方網址get Acquire::http::Proxy "http://aptcache ...

  9. [MAVEN]二、常用命令

    mvn eclipse:eclipse :生成 Eclipse 项目文件,生成后可以导入到eclipse中使用 mvn install :在本地 Repository 中安装 jar ,若是Web项目 ...

  10. ASP.NET MVC 必须设置 ErrorMessageString 或 ErrorMessageResourceName,但不能同时设置二者。

    解决方案: 1.此错误是指你的验证错误信息为空(required="")和required提示信息一致,引发的错误. 简单的来说就是两个验证错误提示消息一样了. 2.修改提示消息解 ...