lovecn / lovecn.github.io

个人记录
5 stars 5 forks source link

php 上传多个文件 #48

Open lovecn opened 7 years ago

lovecn commented 7 years ago
<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
                )
        )
)
数组方式进行表单提交http://www.cnblogs.com/iforever/p/4222595.html 
<form id="form1" action="./index.php" method="get">
    <div class="form-control">
        <input type="text" name="infos[name][]" />
        <input type="text" name="infos[num][]" />
        <input type="text" name="infos[img][]" />
    </div>
    <br>
    <div class="form-control">
        <input type="text" name="infos[name][]" />
        <input type="text" name="infos[num][]" />
        <input type="text" name="infos[img][]" />
    </div>
    <br>
    <div class="form-control">
        <input type="text" name="infos[name][]" />
        <input type="text" name="infos[num][]" />
        <input type="text" name="infos[img][]" />
    </div>
    ......
  <input type="submit" value="Submit" />
</form>
![image](https://cloud.githubusercontent.com/assets/6429263/20640628/477eb10a-b41e-11e6-956a-ba3847b63227.png)

http://php.net/manual/zh/features.file-upload.post-method.php 
foreach ($_FILES["attachment"]["error"] as $key => $error)
{
       $tmp_name = $_FILES["attachment"]["tmp_name"][$key];
       if (!$tmp_name) continue;

       $name = basename($_FILES["attachment"]["name"][$key]);

    if ($error == UPLOAD_ERR_OK)
    {
        if ( move_uploaded_file($tmp_name, "/tmp/".$name) )
            $uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
        else
            $errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
    }
    else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
}
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
                )
        )
)