PHP 多文件数组

标签 php arrays file upload

我有以下代码可以工作和上传,但它不会循环遍历数组来上传每个文件,只上传第一个文件。

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

相关文章:

c - C 中 char* 类型元素的地址

java - 如何将字符串的多个部分放入列表中?

java - Spring ,Java : Streaming file download for avoiding out of memory errors

php - PHP 操作码缓存是否与 __autoload 一起使用?

php - mac os php intratactive模式没有提示

php - 从 json 对象创建 php 类树

file - 如何存储数百万张大小约为 2k 的图片

php - 该数据是否被另一个组件覆盖?

javascript - 使用 Javascript 从 json 数据动态嵌套 ul\li 列表

c# - 上传时网络客户端超时