How to accumulate strings in a single variable?

0

I'm trying to retrieve the attachment names in a single variable and then print them all together. Note that each file is already uploaded to the upload folder. So this is not the case. I really just want to retrieve the names of those attachments, that's all. How to do this? These attachments see from an imput file multiple then in a loop sending all the files to the upload folder successfully. But I want to take advantage of the loop to retrieve the name.

<?php  

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

   for ($k = 0; $k < count($arquivo['name']); $k++){

       $destino = $diretorio."/" . date("d.m.Y-H.i.s"). $arquivo['name'][$k];

       $acumulaNome = ; // Como ficaria esta variavel que acumula os nomes apenas?

       if (move_uploaded_file($arquivo['tmp_name'][$k], $destino)) {echo "Sucesso"; }

       else {echo "erro";}
    }   

?>
    
asked by anonymous 02.03.2016 / 17:36

2 answers

2

If you want a suggestion ,

Remove this variable as it is useless.

$acumulaNome = ;

The names of the files are already in the $arquivo['name'] variable.

So to print these names, you just have to do something like this

echo implode(', ', $arquivo['name']);

Print file names separated by commas.

If you want to continue as it is , forget the implode () and do just that

$acumulaNome .= $arquivo['name'].', ';

To print, make echo rtrim($acumulaNome, ', ');

The rtrim() is to remove the comma that is left over at the end.

    
02.03.2016 / 17:48
0

In PHP there are two alteratives.

You can accumulate with array .

Example:

$acumula = [];

for ($i = 0; $i < 10; $i++) {
    $acumula[] = $i;
}

Result:

[
     1,
     2,
     3,
     4,
     5,
     6,
     7,
     8,
     9,
]

Or with a concatenating a string .

$acumula = '';

for ($i = 0; $i < 10; $i++) {
    $acumula .= $i;
}

Result:

"123456789"

I believe that in your case, the most feasible would be to combine the grouping with array , using implode later:

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

$acumulaNome = [];

for ($k = 0; $k < count($arquivo['name']); $k++){

    $destino = $diretorio."/" . date("d.m.Y-H.i.s"). $arquivo['name'][$k];

    $acumulaNome[] = $arquivo['name'][$k]; // Como ficaria esta variavel que acumula os nomes apenas?

    if (move_uploaded_file($arquivo['tmp_name'][$k], $destino)) {

        echo "Sucesso"; 
    } else {
        echo "erro"; 
    }
} 


echo implode(', ', $acumulaNome);
    
02.03.2016 / 17:47