php多文件上传-input中name的数组命名方法

在多个文件上传中将input中name用数组格式命名方法的使用

<table>
    <form action="" method="POST" enctype="multipart/form-data">
        <tr>
            <td>文件上传:</td>
            <td><input type="file" name="upfile[]" value="浏览..."></td>
        </tr>
        <tr>
            <td>文件上传:</td>
            <td><input type="file" name="upfile[]" value="浏览..."></td>
        </tr>
        <tr>
            <td>文件上传:</td>
            <td><input type="file" name="upfile[]" value="浏览..."></td>
        </tr>
        <tr>
            <td><input type="submit" name="submit"></td>
        </tr>
    </form>
</table>
<?php 
    header("Content-type:text/html;charset=utf-8");
    
    if(!empty($_FILES['upfile']['name'])){

        $array = $_FILES['upfile'];
        $filename = $_FILES['upfile']['name'];
        $file_tmp_name = $_FILES['upfile']['tmp_name'];

        print_r($array);
        echo "<br/>";
        print_r($filename);
        echo "<br/>";
        print_r($file_tmp_name);

        
        for($i = 0; $i < count($filename); $i++){
            move_uploaded_file($file_tmp_name[$i], $filename[$i]);
        }    

    }
    

 ?>
View Code

使用$_FILES['upfile']获取文件数组,通过输出我们可以看到数组中的键,下面是提交后3处输出的效果:


# 输出结果:

Array ( [name] => Array ( [0] => 4.jpg [1] => 5.jpg [2] => 6.jpg ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg [2] => image/jpeg ) [tmp_name] => Array ( [0] => D:\wamp\tmp\phpC71.tmp [1] => D:\wamp\tmp\phpC72.tmp [2] => D:\wamp\tmp\phpC73.tmp ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [size] => Array ( [0] => 102432 [1] => 37172 [2] => 322463 ) )

# 输出结果:

Array ( [0] => 4.jpg [1] => 5.jpg [2] => 6.jpg )

# 输出结果:

Array ( [0] => D:\wamp\tmp\phpC71.tmp [1] => D:\wamp\tmp\phpC72.tmp [2] => D:\wamp\tmp\phpC73.tmp )

 

posted on 2016-11-23 17:19  源人  阅读(539)  评论(0编辑  收藏  举报

导航