How to count certain values within a POST?

0

I am sending a set of files through POST, what I need is to have PHP how many images are inside that POST, what is sent is of this type:

file[]: 554f2b2f7d00f90fa24cfcd4f1a1de19.jpg
file[]: 75554a79325ebd3342c27c7bb7d3ffc4.txt
file[]: 17623f055ea101e2da38b1e0a33ee01c.png
file[]: ac40d5b9ac52f8d7c751694e460cd087.png
    
asked by Patricio 16.12.2018 в 21:57
source

1 answer

1

To know how many POSTs have been done, it would be:

$numero_archivos = count($_POST);
echo $numero_archivos;

To know how many POSTs in the same location:

$archivos = $_POST['file'];
$num_archivos =  count($archivos);
echo $num_archivos;
  

The latter is because in a position to be able to save more than one   value in a POST space the variable $ files is declared as a   array.

Account only files that are images (only png and jpg):

<?php

$contador=0;
$ficheros = $_POST['file'];

foreach($ficheros as $fichero){
  $pos_punto = strpos($fichero, ".");
  $formato = substr($fichero, $pos_punto+1);
  if($formato=="png"||$formato=="jpg"){
    contador++;
  }
}

echo "Hay ".$contador." archivos de imagenes.";

?>
    
answered by 16.12.2018 / 22:18
source