how to count number of input file elements of html with php?

Asked

Viewed 755 times

1

need to count the number of input file elements, as they are created dynamically, with the click of a button

  <input class="tf" type="file" name="pic[]" accept="image/*">      

this is the line that is created dynamically, but I need to count the number of elements that were created, does not work with Count, as I can do?

this is my php code, with the variable paragraph already right.

  <?php

$paragrafo  = isset($_POST['paragrafo']) ? $_POST['paragrafo'] : '';

$foto = isset($_FILES['pic']) ? $_FILES['pic'] : '' ;

echo count($_POST['pic']);

if(count($paragrafo) > 0 && $paragrafo != ''){
    foreach($paragrafo as $item){
        echo  $item .'<br>';
    }
} else {
    echo "Nenhum paragrafo foi adicionado!";
}

?>

  • 1

    This is a input file normally returns a simple array, for a single selected file, and returns a multi. when set Multiple. The point is ?

  • i want to count the number of inputs that were created in my html

  • Fields are generated via php ?

  • no, via jquery/javascript plugin

  • But the input of the kind file usually it is only one.

  • as you click the add image button, with id="add-img"

  • is that I will have to generate a pdf document, and this will have to have images and paragraphs, necessarily having to have the order that would be presented on the screen, among paragraphs and images...

  • I, not to have to think too much, would add in the JS function that adds the images one call to another function that increments a numerical value in a hidden field. every time you add vc would have the exact value of the fields in your DOM

Show 3 more comments

1 answer

-1

if this is your $_FILES['pic' input] then you only have one input, if you want to count the images inside it makes a sizeof($_FILES['pic']) and if you want to go through the images do a foreach($_FILES['pic'] as $file){ }

  • is that I create dynamically, with the same name, various inputs, but I will try like this, as you said

Browser other questions tagged

You are not signed in. Login or sign up in order to post.