How to pick up and count images with php?

Asked

Viewed 621 times

1

How can I count and take, in php, the image of each of these elements of the code below? I CANNOT use an input file 'Multiple', as it will not only be these lines, as below, it will be something 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/*">
  • Please avoid long discussions in the comments; your talk was moved to the chat

2 answers

0


Itere the array received from 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.
}

0

PHP groups files sent with the same name in an intuitive way, I created a function (https://gist.github.com/pedrosancao/9100827) which interprets the received values in a way that allows the treatment as suggested in the documentation Upload files with the POST method be the same regardless if you receive one or more files.

By 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

Also how the error was reported Undefined index: pic your form should not contain the attribute enctype="multipart/form-data", which must be causing part of the problem.


Function setting in case the above link 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;
}

Browser other questions tagged

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