PHP 多文件数组 [英] PHP Multiple File Array

查看:42
本文介绍了PHP 多文件数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code which works and uploads but it will not cycle through the array to upload every file, just the first file.

<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);
        }
?>

我需要它来循环我的文档[]文件数组.

I need it to cycle through my documents[] file array.

文档数组的示例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 ) 
    )

感谢任何帮助.

推荐答案

你可以使用我更新的代码,根据我的演示,它非常适合多个文件上传

you can use my updated code and as per my demo it is working perfect for multiple file upload

 <?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 多文件数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆