• PHP同时上传“多个”文件示例,并格式化$_FILES数组信息


    方法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:PHPxampp2	mpphpF7E1.tmp
                        [1] => D:PHPxampp2	mpphpF7E2.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:PHPxampp2	mpphpF93A.tmp
                        [1] => D:PHPxampp2	mpphpF93B.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:PHPxampp2	mpphp2247.tmp
                [error] => 0
                [size] => 98791
            )
     
        [upfile_2] => Array
            (
                [name] => JDK_API_1_6中文帮助.CHM
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphp2248.tmp
                [error] => 0
                [size] => 36830335
            )
     
        [upfile_3] => Array
            (
                [name] => jquery1.7.2中文手册.chm
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphp23B0.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:PHPxampp2	mpphp4440.tmp
                        [1] => D:PHPxampp2	mpphp4441.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:PHPxampp2	mpphp459A.tmp
                        [1] => D:PHPxampp2	mpphp459B.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:PHPxampp2	mpphp459C.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:PHPxampp2	mpphp4687.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:PHPxampp2	mpphpF27F.tmp
                [error] => 0
                [size] => 98791
            )
     
        [1] => Array
            (
                [key] => upfile
                [name] => JDK_API_1_6中文帮助.CHM
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphpF280.tmp
                [error] => 0
                [size] => 36830335
            )
     
        [2] => Array
            (
                [key] => upfile_2
                [name] => jquery1.7.2中文手册.chm
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphpF3C9.tmp
                [error] => 0
                [size] => 306357
            )
     
        [3] => Array
            (
                [key] => upfile_2
                [name] => jQuery1.8.3中文手册.chm
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphpF3CA.tmp
                [error] => 0
                [size] => 405941
            )
     
        [4] => Array
            (
                [name] => php_manual_zh.chm
                [type] => application/octet-stream
                [tmp_name] => D:PHPxampp2	mpphpF3CB.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:PHPxampp2	mpphpF4C6.tmp
                [error] => 0
                [size] => 1304181
                [key] => upfile_4
            )
     
    )
     
    
  • 相关阅读:
    Educational Codeforces Round 15 C. Cellular Network(二分)
    HDU 1044 Collect More Jewels(BFS+DFS)
    NBOJv2 Problem 1009 蛤玮的魔法(二分)
    HDU 1016 Prime Ring Problem(经典DFS+回溯)
    HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
    OpenCV学习笔记——滑动条开关
    廖雪峰Java15JDBC编程-3JDBC接口-3JDBC更新
    廖雪峰Java15JDBC编程-3JDBC接口-1JDBC简介
    廖雪峰Java15JDBC编程-2SQL入门-2insert/select/update/delete
    廖雪峰Java15JDBC编程-2SQL入门-1SQL介绍
  • 原文地址:https://www.cnblogs.com/qingsong/p/5003720.html
Copyright © 2020-2023  润新知