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