我有以下代码可以工作和上传,但它不会循环遍历数组来上传每个文件,只上传第一个文件。
<form method="post" enctype="multipart/form-data" action="http://<?php echo $pageURL;?>">
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input type="submit" class="button" name="addMaterials" value="Add" />
<?php
foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"files/".time().$file_name);
}
?>
我需要它来循环访问我的 documents[] 文件数组。
文档数组的示例 print_r()
:
Array (
[name] => Array ( [0] => AcroRd32.exe )
[type] => Array ( [0] => application/x-msdownload )
[tmp_name] => Array ( [0] => C:\xampp\tmp\phpE8BD.tmp )
[error] => Array ( [0] => 0 )
[size] => Array ( [0] => 1343112 )
)
感谢任何帮助。
最佳答案
您可以使用我更新的代码,根据我的演示,它非常适合多个文件上传
<?php
if(isset($_FILES['documents'])){
foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['documents']['name'][$key];
$file_size =$_FILES['documents']['size'][$key];
$file_tmp =$_FILES['documents']['tmp_name'][$key];
$file_type=$_FILES['documents']['type'][$key];
move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";
echo "</form>";
}
?>
关于PHP 多文件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941592/