方法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. 一个简单的统计图像主颜色的算法(C#源代码)

    前段日子有朋友咨询了下分析图像主颜色的算法,我对这一块也没有什么深入的研究,参考了一些小代码,然后自己写了一个很简单的小工具,现共享给大家. 界面截图如下: 算法的原理很简单,就是统计出图像中各种颜色 ...

  2. VB.NET中图像处理的一些技巧以及其和C#图像处理的差距。

    早期的时候我使用的开发工具是VB6,VB6做图像处理的速度在我的软件Imageshop中有所体现,还是算可以的.目前,我已经改用C#来研究图像算法,C#中有指针,做图像处理起来效率确实要高不少.VB. ...

  3. WinCE项目应用汇总

    虚拟仪器(VI) RM905a+医用放射性核素活度计 RM905a+活度计远程检定方法研究 车载导航

  4. 解决某些Android Permission denied

    最近遇到一个问题,总是在模拟器重报Permission denied错误,于是我直接在手机上测试,发现没有错误,于是很郁闷,反复在AndroidManifest中加入权限   <uses-per ...

  5. Angular指令2

    scope Create a new scope for this directive rather than inheriting the parent scope. controller Crea ...

  6. Cache,MemCache,Application,Cookie等其它缓存汇总

    为了提高网页运行速度我们用到了各种缓存技术,今天就来汇总下,相信聪明的你也一定会有所收获的o(^▽^)o 1.Cache(PS:看见一个博客介绍的不错,我就直接Copy了) Cache 即高速缓存.那 ...

  7. 1125MySQL Sending data导致查询很慢的问题详细分析

    -- 问题1 tablename使用主键索引反而比idx_ref_id慢的原因EXPLAIN SELECT SQL_NO_CACHE COUNT(id) FROM dbname.tbname FORC ...

  8. servlet的四个作用域

    作用域规定的是变量的有效期限,servlet有四个作用域对象,这里只说三个: 一. request作用域: 1.作用范围: 就是指从http请求发起,到服务器处理结束,返回响应的整个过程.在这个过程中 ...

  9. iOS --- DIY文件名批量修改

    批量修改文件名: // 1.创建文件管理 NSFileManager *filemanager =[NSFileManager defaultManager]; // 2. 获得所有文件夹路径 NSS ...

  10. python 小练手

    监控 主动监控 - 服务器端轮询客户端 被动监控-客户端agent上报到服务器端 混合模式---两种都支持 需求 1个性化的监控需求 2每个服务的监控间隔不同 3混合模式的监控