How to get and count images with php?

1

How can I count and get the image of each of these code elements in php? I can not use a 'multiple' input file because it will not only be these lines, as below, it will be somewhat dynamic, and it may contain other elements (textarea for example) in the middle of these lines

                <input class="tf" type="file" name="pic[]" accept="image/*">
                <input class="tf" type="file" name="pic[]" accept="image/*">
                <input class="tf" type="file" name="pic[]" accept="image/*">
                <input class="tf" type="file" name="pic[]" accept="image/*">
    
asked by anonymous 26.10.2015 / 22:51

2 answers

0

Itere the received array of the global variable $_FILES

// Obtém o tamanho do array, ou seja, a quantidade de imagens recebidas
$count = count($_FILES['pic']['tmp_name']);

// Faz a iteração
for ($i = 0; $i < $count; $i++) {
    $file_name = $_FILES['pic']['name'][$i]; // nome do arquivo
    $file_type = $_FILES['pic']['type'][$i]; // tipo
    $file_error = $_FILES['pic']['error'][$i]; // código de erro do upload, se houver
    $file_size = $_FILES['pic']['size'][$i]; // tamanho em bytes
    $file_path = $_FILES['pic']['tmp_name'][$i]; // caminho completo do path temporário.
}
    
28.03.2016 / 21:22
0

PHP groups uploaded files with the same name in a less intuitive way, I've created a function ( link ) that interprets the received values in a way that allows for the treatment as suggested in the Uploading Files with Method POST is the same regardless of whether you receive one or more files.

When calling parse_files() , a numeric suffix is added to the form field name, so that the data is as follows:

// estrutura original 
echo $_FILES['pic']['name'][0]; // exemplo1.jpg
echo $_FILES['pic']['name'][1]; // exemplo2.jpg
// estrutura depois do tratamento
$files = parse_files();
echo $files['pic_0']['name']; // exemplo1.jpg
echo $files['pic_1']['name']; // exemplo2.jpg

In addition, as the Undefined index: pic error was reported, your form should not contain the enctype="multipart/form-data" attribute, which should be causing some of the problem.

Function definition if link above is inaccessible.

<?php
/**
 * Parses the PHP's $_FILES array when multiple file input is used, it will
 * return an array as each file was from a different input. It also works
 * when multiple and single inputs are mixed
 * 
 * @author   Pedro Sanção
 * @license  MIT Licence
 */
function parse_files() {
    $files = array();
    foreach ($_FILES as $fieldName => $file) {
        reset($file);
        $key = key($file);
        if (is_array($file[$key])) {
            $propeties = array_keys($file);
            $fieldNamekeys = array_keys($file[$key]);
            foreach ($fieldNamekeys as $namekey) {
                $fileKey = "{$fieldName}_{$namekey}";
                $files[$fileKey] = array();
                foreach ($propeties as $property) {
                    $files[$fileKey][$property] = $file[$property][$namekey];
                }
            }
        } else {
            $files[$fieldName] = $file;
        }
    }
    return $files;
}
    
27.10.2015 / 13:30