方法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. Oozie coordinator 作业自定义的配置的一些方法

    Oozie的coordinator有啥用? The Oozie Coordinator system allows the user to define and execute recurrent a ...

  2. 洛谷P3388 【模板】割点

    给出一个n个点,m条边的无向图,求图的割点. u是cut vertex的两个条件: 1.存在v使v及其所有后代没有反向边连回u的祖先 2.u是根且有两个以上子节点 dfs一遍 low[u]是u及其后代 ...

  3. 第九章 JQUI

    一.什么是插件 ①是遵循一定接口规范编写的程序 ②是原有系统平台功能的扩展和补充 ③只能运行在规定的系统平台下,而不能单独运行 注:由于jQuery插件是基于jQuery脚本库的扩展,所以所有jQue ...

  4. etl实现字段值相加

    数据库USERS表: etl步骤: (2) (3) 其中java代码为: import test.Test;          public boolean processRow(StepMetaIn ...

  5. 使用adb shell 进入手机修改文件的权限

    1.将android的tools目录加入到path中,或者直接在adb.exe路径下启动cmd窗口2.adb shell 进入手机后,发现是 $ ,不是 # 号3.在进入shell后运行 su ,就可 ...

  6. LeetCode:Count and Say

    题目链接 The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 11 ...

  7. testng 6.8.6 eclipse plugin

    http://files.cnblogs.com/mikelij/testng.zip

  8. SQLite剖析之事务处理技术

    前言 事务处理是DBMS中最关键的技术,对SQLite也一样,它涉及到并发控制,以及故障恢复等等.在数据库中使用事务可以保证数据的统一和完整性,同时也可以提高效率.假设需要在一张表内一次插入20个人的 ...

  9. 利用ucenter整合discuz数据

    由于项目需要,需要用到discuz论坛用户的数据,所以想到利用ucenter获取数据.以下为整合ucenter的过程. 1.首先你需要下载官方demo:http://faq.comsenz.com/l ...

  10. js ie中实现拖拽

    获取鼠标移动信息 开始我们需要获取鼠标的坐标.我们添加一个document.onmousemove 就可以达到此目的: Javascript:   document.onmousemove = mou ...