1

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] => 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?

1
  • 1
    just a note: all 5 inputs have the same ID Commented Jan 15, 2010 at 0:07

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

3 Comments

Minor nitpick, but the === true checks are unnecessarily verbose. None of the functions in question are capable of returning anything that would evaluate to true, other than true itself. You might as well check that 1+1 === 2
This recommendation relies on Javascript, which may not be turned on in every browser.
@Nilpo: It doesn't rely on Javascript, it only suggests that by using Javascript one could avoid having hundreds of <input type="file" name="file[]" id="file" /> showing up by default, the second paragraph and everything below still apply anywhere.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.