开发者

Uploading multiple files with PHP

This is my HTML markup:

<p><input type="file" name="file[]" id="file" /></p>
<p><input type="file" name="file[]" id="file" /></p>
<p><input type="file" name="file[]" id="file" /></p>
<p><input type="file" name="file[]" id="file" /></p>
<p><input type="file" name="file[]" id="file" /></p>

When I submit the form, the form is submitting empty files. This is a print_r of the array it sends:

Array
(
    [name] => Array
        (
            [0] => thumb.jpg
            [1] => 
            [2] => 
            [3] => 
            [4] => 
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => 
            [2] => 
            [3] => 
            [4] => 
        )

    [tmp_name] => Array
        (
            [0]开发者_如何学JAVA => C:\xampp\tmp\phpEE16.tmp
            [1] => 
            [2] => 
            [3] => 
            [4] => 
        )

    [error] => Array
        (
            [0] => 0
            [1] => 4
            [2] => 4
            [3] => 4
            [4] => 4
        )

    [size] => Array
        (
            [0] => 5130
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

)

Is there a way to stop all these blank files being sent? As I have a feeling it's going to give me a headache later down the script. I thought I could check the error code for each file (4 if there is no file) and then unset it from the array, what do you think?


KISS it. Just check the error codes and be done with it.
No simple way around this as users (they are stupid, you know) will try to upload, some times only in the last position, or may be in the first and third position...you get my drift.
B.T.W there are many ready made classes out there which can handle this for you.


One way to do it is to have only one file upload input and a + button that calls a JavaScript function that adds another (and another...) file upload input.

I don't see why the extra array values would give you problems though, just use a generic upload function like the following and you should be fine:

function Upload($source, $destination)
{
    $result = array();

    if (array_key_exists($source, $_FILES) === true)
    {
        $destination = str_replace('\\', '/', realpath($destination));

        if (is_array($_FILES[$source]['error']) === true)
        {
            foreach ($_FILES[$source]['error'] as $key => $value)
            {
                if ($value == UPLOAD_ERR_OK)
                {
                    if (move_uploaded_file($_FILES[$source]['tmp_name'][$key], $destination . basename($_FILES[$source]['name'][$key])) === true)
                    {
                        $result[] = $destination . basename($_FILES[$source]['name'][$key]);
                    }
                }
            }
        }

        else
        {
            if (move_uploaded_file($_FILES[$source]['tmp_name'], $destination . basename($_FILES[$source]['name'])) === true)
            {
                $result[] = $destination . basename($_FILES[$source]['name']);
            }
        }
    }

    return $result;
}


Perhaps you could check field values (input.value != "") before submitting the form and remove them from the DOM if empty.

Or, server-side, check error code against UPLOAD_ERR_NO_FILE constant.


You could start with just the one file input hard-coded into the html then allow users to click a button that calls a javascript function to add an extra input for each additional file they want to upload. That way there shouldn't be any unnecessary file inputs. Similarly you could you use javascript to test for empty inputs when the form is submitted and remove them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜