博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP同时上传“多个”文件示例,并格式化$_FILES数组信息
阅读量:6803 次
发布时间:2019-06-26

本文共 6701 字,大约阅读时间需要 22 分钟。

方法1:

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

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

输出结果:

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

服务端“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        ))

 


 

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

服务端接收到的数据为:

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        ))

 

 

延伸阅读:

 

转载地址:http://bejwl.baihongyu.com/

你可能感兴趣的文章
mongodb
查看>>
CMAKE总结(1) .lib .dll .a .so libx.dll libx.dll.a
查看>>
java读取配置文件*.property
查看>>
how to send mail from 3rd
查看>>
mappingResources、mappingLocations、mappingDirectoryLocations、mappingJarLocations
查看>>
AJAX 传递jison数组 ;前端循环辅助数组 -----解决方案
查看>>
关于磁盘相关知识
查看>>
Mozilla推出HTML/CSS在线交互式学习网站Thimble
查看>>
关于ppp认证
查看>>
彻底禁用SilentDetection.aspx,极速登录
查看>>
为了忘却的纪念,我的天龙游戏生涯
查看>>
12294错误事件的处理--利用审核日志查找病毒来源
查看>>
第25讲: Scala中柯里化实战详解
查看>>
81.LAMP,PHP5和PHP7安装
查看>>
linux服务(一)LAMP编译安装
查看>>
一次RPC调用时间都去哪儿了
查看>>
linux的rsync工具的常用选项及ssh同步介绍
查看>>
oracle内存体系(二)
查看>>
ReflectASM的使用
查看>>
智能家居监控移动手机组态现实生活中的应用
查看>>