Ankit Chhatbar
Ankit Chhatbar

Reputation: 342

Unexpected Count Value in PHP

I have file uploader

<input type="file" name="photos[]" id="files" multiple="true" />

and I need the total no of files uploaded in uploader.

echo count($_FILES['photos']['name']);

I haven't applied any increment in this.

Why this happening,I am totally confused...

Upvotes: 1

Views: 186

Answers (1)

Kevin
Kevin

Reputation: 41885

This is should give a better idea on why that's happening.

When you have (for example) two of those fields and submitted it without putting any files. This is what $_FILES would look like:

Array
(
    [photos] => Array
        (
            [name] => Array
                (
                    [0] => 
                    [1] => 
                )

            [type] => Array
                (
                    [0] => 
                    [1] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                    [1] => 
                )

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

            [size] => Array
                (
                    [0] => 0
                    [1] => 0
                )

        )

)

So now, upon submission, your are counting echo count($_FILES['photos']['name']); which equates to 2. But the fields are empty.

Inside error index are actually codes, and they are interpreted here:

http://php.net/manual/en/features.file-upload.errors.php

That two 4's out there means:

UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.

Upvotes: 2

Related Questions