Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php 上传多个文件 #48

Open
lovecn opened this issue Nov 26, 2016 · 0 comments
Open

php 上传多个文件 #48

lovecn opened this issue Nov 26, 2016 · 0 comments

Comments

@lovecn
Copy link
Owner

lovecn commented Nov 26, 2016

<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
                )
        )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant