V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
helloccav
V2EX  ›  PHP

请教 php 上传多个文件的问题

  •  
  •   helloccav · 2016-11-19 18:17:07 +08:00 · 2202 次点击
    这是一个创建于 2721 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表单 HTML :

    <form method="post" enctype="multipart/form-data">
        <input type="file" name="file[]" multiple="multiple">
    </form>
    

    php 端通过 $_FILES 获取到的值:

    Array
    (
        [file] => Array
            (
                [name] => Array
                    (
                        [0] => 1.jpg
                        [1] => 2.jpg
                    )
                [type] => Array
                    (
                        [0] => image/jpeg
                        [1] => image/jpeg
                    )
                [tmp_name] => Array
                    (
                        [0] => /tmp/php330E.tmp
                        [1] => /tmp/php330F.tmp
                    )
    
                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 59420
                        [1] => 300300
                    )
            )
    )
    

    请问 html 元素里有什么属性可以让 php 端通过 $_FILES 获取到的值变成这样,或者 php 里有什么非常简便的方法、函数可以变成这样:

    Array
    (
        [file] => Array
            (
                [0] => Array
                    (
                        [name] => 1.jpg
                        [type] => image/jpeg
                        [tmp_name] => /tmp/php330E.tmp
                        [error] => 0
                        [size] => 59420
                    )
                    
                [1] => Array
                    (
                        [name] => 2.jpg
                        [type] => image/jpeg
                        [tmp_name] => /tmp/php330F.tmp
                        [error] => 0
                        [size] => 300300
                    )
            )
    )
    
    9 条回复    2016-11-20 16:29:47 +08:00
    hanzichi
        1
    hanzichi  
       2016-11-19 18:30:32 +08:00
    一般都是用 name 属性吧 ...
    liuxu
        2
    liuxu  
       2016-11-19 23:03:57 +08:00
    lslqtz
        3
    lslqtz  
       2016-11-20 02:16:37 +08:00
    我也想知道一下。
    @hanzichi 这些字段是文件的信息,没法通过指定 name 来改...
    lslqtz
        4
    lslqtz  
       2016-11-20 02:45:09 +08:00
    楼主,提个问题,这样你会丢失掉传过来的 name 名。
    lslqtz
        5
    lslqtz  
       2016-11-20 02:50:23 +08:00   ❤️ 1

    我就只能做到这样了,效率什么的。。懒得管了
    <form method="post" enctype="multipart/form-data">
    <input type="file" name="f1">
    <input type="file" name="f2">
    <input type="submit">
    </form>
    <?php
    if (count($_FILES) != 1) {
    $filekeys=array_keys($_FILES);
    for ($i=0;$i<count($filekeys);$i++) {
    $nowfile=$_FILES[$filekeys[$i]];
    $files['file'][$i]=['name'=>$nowfile['name'],'type'=>$nowfile['type'],'tmp_name'=>$nowfile['tmp_name'],'error'=>$nowfile['error'],'size'=>$nowfile['size']];
    }
    unset($filekeys,$nowfile);
    }
    print_r($files);
    ?>
    lslqtz
        6
    lslqtz  
       2016-11-20 02:51:53 +08:00
    你可以把它用 function 写成一个函数,然后传出数组来
    lslqtz
        7
    lslqtz  
       2016-11-20 02:52:44 +08:00
    不过似乎。。忘了考虑单文件下的了, if 后面加个 else 来直接 print 或者拼起来吧
    chaegumi
        8
    chaegumi  
       2016-11-20 08:25:37 +08:00   ❤️ 2
    不用自己写,用现成的组件 http://fex.baidu.com/webuploader/
    duo6duo6
        9
    duo6duo6  
       2016-11-20 16:29:47 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2116 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:55 · PVG 18:55 · LAX 03:55 · JFK 06:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.