Upload one or more files to server received in a json

1

I receive a data json, which may contain one or several attachments, all of them will receive the file in base64. I give you an example:

{
"id": 1,
"data": {
    "nombre": "Nombre",
    "file1": "Mhuc2dvCg==",
    "file2": "Yhj4dysYn==",
    "file3": "Jusj6Hism==",
    "file4": "Rhsy7Yhah==",
    ...
    }
}

I have managed to upload a file, but I do not know how to make it identify each one of those who arrive in the json. That is, if I do:

$adjunto1 = isset($input['data']['file1']) ? $input['data']['file1'] : '';

And after that I call the function subirFichero($adjunto1); everything works correctly. But how do I recognize all the files that arrive in the json, that is, to check that if it starts with "file" it is an attached file and I insert them?

    
asked by Norak 26.06.2018 в 17:51
source

2 answers

2

You have two solutions depending on whether you can / want to improve or not the structure of the JSON data sent:

Maintaining the JSON format

You can iterate through each element using a foreach and check that the index is not nombre :

<?php
/* Reproducimos la variable $input: */
$input = json_decode('
{
    "id": 1,
    "data": {
        "nombre": "Nombre",
        "file1": "Mhuc2dvCg==",
        "file2": "Yhj4dysYn==",
        "file3": "Jusj6Hism==",
        "file4": "Rhsy7Yhah=="
    }
}
', true);

/* Simulamos tu función agregando información de depuración */
function subirFichero($base64) {
    echo '<p>BASE64 recibido: ', $base64, '</p>', PHP_EOL;
}

/* Enumeramos los elementos e ignoramos el que se llame "nombre" */
foreach ($input['data'] as $clave => $base64) {
    if ($clave !== 'nombre') {
        subirFichero($base64);
    }
}

The output obtained is:

BASE64 recibido: Mhuc2dvCg==
BASE64 recibido: Yhj4dysYn==
BASE64 recibido: Jusj6Hism==
BASE64 recibido: Rhsy7Yhah==

Optimizing the JSON format

Normally, the content of the JSON facilitates its analysis by the application that receives the data, therefore the ideal is to adjust its content to a property that enumerates the files unequivocally:

{
    "id": 1,
    "data": {
        "nombre": "Nombre",
        "files": [
            "Mhuc2dvCg==",
            "Yhj4dysYn==",
            "Jusj6Hism==",
            "Rhsy7Yhah=="
        ]
    }
}

So the PHP code is greatly simplified:

<?php
$input = json_decode('
{
    "id": 1,
    "data": {
        "nombre": "Nombre",
        "files": [
            "Mhuc2dvCg==",
            "Yhj4dysYn==",
            "Jusj6Hism==",
            "Rhsy7Yhah=="
        ]
    }
}
', true);

/* Simulamos tu función agregando información de depuración */
function subirFichero($base64) {
    echo '<p>BASE64 recibido: ', $base64, '</p>', PHP_EOL;
}

/* Enumeramos cada uno de los elementos */
foreach ($input['data']['files'] as $base64) {
    subirFichero($base64);
}

The output obtained is (as before):

BASE64 recibido: Mhuc2dvCg==
BASE64 recibido: Yhj4dysYn==
BASE64 recibido: Jusj6Hism==
BASE64 recibido: Rhsy7Yhah==
    
answered by 14.08.2018 / 10:38
source
0

You could do it using an array and a foreach like this:

$files = array()
foreach($input['data'] as $key => $value) {
    if(strpos($key, 'file') !== false) {
        $files[] = $value;
    }
}

Or if you want, you can upload the files inside the loop like this:

foreach($input['data'] as $key => $value) {
    if(strpos($key, 'file') !== false) {
        subirFichero($value);
    }
}
    
answered by 26.06.2018 в 18:02