方法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. CentOS安装MySQL

    好记性不如烂笔头,记录一下 yum list installed | grep mysql #检查是否安装了mysql yum -y remove mysql-libs.x86_64 #卸载已经安装的 ...

  2. java 异常

    1.java异常 2.自定义抛出 3.运行时异常,程序有问题,让使用者可以改' ' 4.return  和  throw的区别 return 符合函数要求的值    throw  有问题的时候用它结束 ...

  3. BZOJ1856[SCOI2010]字符串

    Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...

  4. Windows下Nginx Virtual Host多站点配置详解

    Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 window ...

  5. windows7配置Nginx+php+mysql教程

    windows7配置Nginx+php+mysql教程 最近在学习php,想把自己的学习经历记录下来,并写一些经验,仅供参考交流.此文适合那些刚刚接触php,想要学习并想要自己搭建Nginx+php+ ...

  6. WPF绘制简单常用的Path

    写代码出身的我们经常需要使用一些简单 但是不是规则图形的Path 但限于美工功底有限 不知道怎么去画 下面我告诉大家一些简单的小技巧 用代码来画Path 个人还是比较喜欢用代码 因为数值控制的更精细 ...

  7. Visual Studio 2015 Pre Secondary Installer 在哪里

    安装vs2015 pre后,会自动打开Secondary Installer, 用于Cross Platform的移动开发框架,包括Cordova插件.若安装失败,启动程序位置: "D:\P ...

  8. ADC

    ADC转换分为两种通道组 1.规则通道组 2.注入通道组(可打断规则通道组) 工作模式 通道模式 转换模式 复位校准 AD校准

  9. iOS开发小技巧--相机相册的正确打开方式

    iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...

  10. 走进AngularJs(二) ng模板中常用指令的使用方式

    通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...