1

How can I count the total items in this array below?

Array
(
    [upload] => Array
        (
            [name] => Array
                (
                    [0] => 1024x768.jpg
                    [1] => 1280x800.jpg
                    [2] => 1280x1024.jpg
                    [3] => 1440x900.jpg
                )

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

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php34FE.tmp
                    [1] => C:\wamp\tmp\php353D.tmp
                    [2] => C:\wamp\tmp\php356D.tmp
                    [3] => C:\wamp\tmp\php35AC.tmp
                )

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

            [size] => Array
                (
                    [0] => 469159
                    [1] => 602230
                    [2] => 739779
                    [3] => 707039
                )

        )

)

this is my method, but I think it's stupid! any better ways/ methods to count the total items inside the array?

<pre>
<?php if ($_FILES) {print_r($_FILES);}?>
</pre>

<?php 
echo count($_FILES['upload']['name']);

if(empty($_FILES['upload']['name'][0]))
{
    echo '0 file has been uploaded.';
}
?>

many thanks, Lau

3
  • 3
    What's stupid about 'count();' ? Commented Jul 23, 2010 at 22:50
  • I don't understand what is wrong here. This is exactly the right way to count the elements. Commented Jul 24, 2010 at 0:47
  • lol probably i had made it too complicated! just it might have another better way... :-) Commented Jul 24, 2010 at 1:45

2 Answers 2

3

Your approach is certainly not stupid. If you want to count the number of uploads that occurred without error, you could foreach through $_FILES['upload']['error'] to ensure they're all 0.

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

2 Comments

Edit = Typo. Checking for actual errors is a great idea here.
@Charles: Thanks for the grammar fix.
1

FYI... the $_FILES array structure will change if you have a form element that is nested.

eg.

<input name="mysuperform[image_file]" type="file" />

Try it and see... also try:

<input name="mysuperform[images][image_file]" type="file" />

Good luck.

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.