Home  >  Article  >  Backend Development  >  PHP文件上传之多文件上传的实现思路,_PHP教程

PHP文件上传之多文件上传的实现思路,_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:59:56853browse

PHP文件上传之多文件上传的实现思路,

多文件上传的两种情况

①使用多个name值

<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type="file" name="file4">

a.点击提交之后接收到的数据格式

Array
(
[file1] => Array
(
[name] => 8.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php737.tmp
[error] => 0
[size] => 200
)
[file2] => Array
(
[name] => 28.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php738.tmp
[error] => 0
[size] => 6244
)
[file3] => Array
(
[name] => 54a296f8n6787b34c.png
[type] => image/png
[tmp_name] => G:\wamp\tmp\php739.tmp
[error] => 0
[size] => 3143
)
[file4] => Array
(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
)
)

从这种格式可以看出来,每一个文件对应一个数组单元

所以使用foreach遍历数组,并对每个数组单元进行文件上传函数调用

b.点击提交后的操作

①接收上传的文件信息

$file = $_FILES;

②引入上传函数

include('./functions.php');

③设置文件保存路径

$path = './uploads/'; // 此目录需要手动创建

④调用文件上传函数

foreach($file as $v){
$info = uploadFile($v,$path);
⑤判断上传状态
if($info['isok']){
echo '上传成功'.$info['message'];
} else {
echo '上传失败'.$info['message'];
}
}

---------------------------------------------------------------

②使用单个name值

a.第一种写法

<input type='file' name="file[]">
<input type='file' name="file[]">
<input type='file' name="file[]">

b.第二种写法

<input type="file" name="file[]" multiple>

c.点击提交之后,接收到的数据格式

Array
(
[userpic] => Array
(
[name] => Array
(
[0] => 8.png
[1] => 9b2d7581fba543ec9bcf95e91018915a.gif
[2] => 12.jpg
)
[type] => Array
(
[0] => image/png
[1] => image/gif
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => G:\wamp\tmp\php85E5.tmp
[1] => G:\wamp\tmp\php85E6.tmp
[2] => G:\wamp\tmp\php8635.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 200
[1] => 16503
[2] => 19443
)
)
)

从这种格式可以看出来,是将上传的文件信息分开保存到每个下标中。
所以要做的事情就是拼接出来一个完整的文件信息,一个一维数组

Array(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
)

所以要进行的操作,是遍历$_FILES['file'] 然后从中取出每条上传文件的信息

d.点击提交后的操作

①接收上传的文件信息

$file = $_FILES['file'];

②引入上传函数

include('./functions.php');

③设置文件保存路径

$path = './uploads/'; // 此目录需要手动创建

④调用文件上传函数

foreach($file['name'] as $key=>$value){
$data['name'] = $file['name'][$key];
$data['type'] = $file['type'][$key];
$data['tmp_name'] = $file['tmp_name'][$key];
$data['error'] = $file['error'][$key];
$data['size'] = $file['size'][$key];
$info = uploadFile($data,$path);

⑤判断上传状态

if($info['isok']){
echo '上传成功'.$info['message'];
} else {
echo '上传失败'.$info['message'];
}
}

a.遍历$file['name'] 只是为了获取$key

b.每遍历一次,取出相对应下标的文件信息,赋值给一个新数组中对应的键

如第一次 $key = 0;

$data['name'] = $file['name'][0]; // 相当于取出了第一个文件的名字
$data['type'] = $file['type'][0]; // 相当于取出了第一个文件的类型

...

第一次遍历完成之后

$data = array(
[name] => 54c0573dncb4db6f7.jpg
[type] => image/jpeg
[tmp_name] => G:\wamp\tmp\php788.tmp
[error] => 0
[size] => 5404
);

这样就取出了第一个文件的所有信息

然后调用上传函数,进行文件上传处理

第二次遍历时$key=1,相当于获取第二个上传文件的信息

您可能感兴趣的文章:

  • PHP5+UTF8多文件上传类
  • php 动态多文件上传
  • 最新的php 文件上传模型,支持多文件上传
  • php中关于普通表单多文件上传的处理方法
  • php多文件上传功能实现原理及代码
  • php jquery 多文件上传简单实例
  • php多文件上传实现代码
  • php多文件上传下载示例分享
  • Thinkphp多文件上传实现方法
  • PHP多文件上传类实例
  • PHP多文件上传实例
  • PHP实现文件上传和多文件上传

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1096142.htmlTechArticlePHP文件上传之多文件上传的实现思路, 多文件上传的两种情况 ①使用多个name值 input type="file" name="file1"input type="file" name="file2"input type="fi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn