首先向大家讲解一下实现的方法。
要实现多文件上传,我们可以在form表单中添加多个input file域,然后将这些input file的name属性设置为相同的名称且使用数组的形式命名,例如filename[]。至于文件上传的php代码和单个文件上传是一样的道理。
下面看一个多文件上传的实例:
html文件example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
</ head >
< body >
< form action = "my_parser.php" method = "post" enctype = "multipart/form-data" >
< p >< input type = "file" name = "file_array[]" ></ p >
< p >< input type = "file" name = "file_array[]" ></ p >
< p >< input type = "file" name = "file_array[]" ></ p >
< input type = "submit" value = "Upload all files" >
</ form >
</ body >
</ html >
|
php文件my_parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
if (isset( $_FILES [ 'file_array' ])){
$name_array = $_FILES [ 'file_array' ][ 'name' ];
$tmp_name_array = $_FILES [ 'file_array' ][ 'tmp_name' ];
$type_array = $_FILES [ 'file_array' ][ 'type' ];
$size_array = $_FILES [ 'file_array' ][ 'size' ];
$error_array = $_FILES [ 'file_array' ][ 'error' ];
for ( $i = 0; $i < count ( $tmp_name_array ); $i ++){
if (move_uploaded_file( $tmp_name_array [ $i ], "test_uploads/" . $name_array [ $i ])){
echo $name_array [ $i ]. " upload is complete<br>" ;
} else {
echo "move_uploaded_file function failed for " . $name_array [ $i ]. "<br>" ;
}
}
}
?>
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!