How to Accumulate strings in a single variable?

Asked

Viewed 1,011 times

0

I’m trying to retrieve attachment names in a single variable and then print them all together. Note that I already send each file to the upload folder. So this is not the case. I really just want to rescue the names of these attachments, nothing else. How to do this? These attachments come from an imput file Multiple then in a loop send all the files to the upload folder successfully. But I want to take advantage of the loop to rescue 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";}
    }   

?>
  • $acumulaNome .= $arquivo['name'][$k] ."\r\n"; - The "\r\n" is the line break, can exchange for "<br>" or "<br>\n" if it is displayed on a web page. Depending on the application, you can use only "\n" that already solves. Probably the implode be the most suitable way, as stated by Omine.

  • 1

    I was until now trying to solve my problem starting with this variable $acumulaNome and I saw your comment. Simply the best answer is fully functional, I did the tests and already list the names, I change the name (thing q could not in the other answers) I receive the email with the list of all names, just send the variable. Fabulous. Thank you very much, if you want to ask the question mark street answer

  • Golx can leave as it is, @Danielomine’s reply is good. The implode(', ', $arquivo['name']); would anyway, only you change the ',' for what I commented above. "\r", or even "<br>\r". Example: AFTER THE LOOP, not inside, you put it like this: $acumulanome = implode( "<br>\n", $arquivo['name'] );

  • Okay. Thank you, if I asked a good question, please mark

2 answers

2


If you want a suggestion,

remove this variable as it is useless.

$acumulaNome = ;

Filenames are already in the variable $arquivo['name'].

So to print out these names would just do something like this

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

Print the names of the comma-separated files.

If you want to continue as you are, forget implode() and do just that

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

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

The rtrim() is to remove the remaining comma at the end.

  • Join is deprecated in PHP. use explode instead.

  • Fix: With the craziness of switching things from php, now she’s a alias for implode (which makes it daunting, as the aliases in php are discouraged). But in the past she was discouraged and released a deprecated :(

  • Daniel, they must have updated or else I got confused with the split. But any function in php that is alias, the documentation usually usually discourage (because it can be removed in future versions). But it is not the subject of the question, so let it go ;)

  • @Danielomine, thank you for your answers, but I was unsuccessful. I can really echo with implode(', ', '$file['name']); there were all the names, but I must be doing something wrong, I’m sending the implode in the body of an email by php Mailler and in the email of arrives the list. how can I email these names in the $file[name]

  • Okay, how should I ask this question? is that I can be negative to Ecas... How to send by phpmailler a variable with accumulated strings?

  • just understand that the subsequent doubt becomes a secondary doubt and was not even described in the question.

Show 1 more comment

0

In PHP there are two changes.

You can accumulate with a array.

Example:

$acumula = [];

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

Upshot:

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

Or with a concatenating one string.

$acumula = '';

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

Upshot:

"123456789"

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

$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);
  • This array option was what I wanted it to work for me but not echo implode(', ', $accumulatName); prints, and I’m getting a "Parse error: syntax error, Unexpected '[' in /home/megah126/public_html/classya/Cadasto/Cadasto.php on line 3.

Browser other questions tagged

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