How to generate an array with the list of uploaded files?

Asked

Viewed 529 times

2

Type I want to generate an array like this.

array('nome1', 'nome2', 'nome3',)

what I have is the $name string, how do I generate the above array ?

would be something like

array($nome)

??

my code

if (!empty($_FILES['files'])) {

            $string = str_replace(' ', '-', $len);

            $new_name = convert_accented_characters($string);

            for($i = 0; $i < $len; $i++) {
                $fileSize = $_FILES['userfile']['name'][$i];

                $string = str_replace(' ', '-', $fileSize);

                $new_name = convert_accented_characters($string);

                echo $new_name.'<br/>';
            }

            //Configure upload.
            $this->upload->initialize(array(
                "file_name" => array($new_name),
                "upload_path"   => './public/uploads/album/'. $past_date .'/'. $pasta .'/',
                "allowed_types" => "mp3",
                "max_size"  => "2194304000"
            ));
  • Can you explain further what you want? It means that you have one/several variables with string values saved and you want to put these strings/variables inside an array?

  • What is the content of your variable $nome?

  • yes, exactly that @Sergio

  • filename of an input @Rodrigorigotti

  • @Uellingtonpalma put an answer. That’s what I was looking for?

4 answers

4


To generate an array the syntax is:

$minha_array = array($elemento1, $elemento2, ..., $elementoN); // onde $elementoX é uma variável, string ou array

In case you have strings already stored inside variables you can do as shown above.

$foo = 'foo';
$bar = 'bar';
$minha_array = array($foo, $bar, 'nova string');
echo $minha_array[0]; // dá 'foo'
echo $minha_array[1]; // dá 'bar'
echo $minha_array[2]; // dá 'nova string'

To add new elements to an array you can use $minha_array[] = $elemento;, this way each new element will be added at the end of the array.

Edit:

Adapting to your code can do so:

$nomes = array();

for($i = 0; $i < $len; $i++) {
    $fileSize = $_FILES['userfile']['name'][$i];
    $string = str_replace(' ', '-', $fileSize);
    $nomes[] = convert_accented_characters($string);
    // echo $new_name.'<br/>';
}

//Configure upload.
$this->upload->initialize(array(
    "file_name" => $nomes,
    // etc...
  • Perfect, and if the $element string is holding several values from a loop ?

  • @Uellingtonpalma in this case within the loop you can have $minha_array[] = $elemento;, this way each new element will be added at the end of the array.

  • I think it fits, I’ll edit my question and you tell me where I fit it.

  • $new_name @Sergio

  • @Uellingtonpalma I edited the answer with your code.

  • 1

    Perfect, thank you

Show 1 more comment

2

From what I understand you want to generate an array containing the list of names of the files that will be sent, if you use empty brackets, so it sets the index automatically:

if (!empty($_FILES['files'])) {

    $string = str_replace(' ', '-', $len);

    $new_name = convert_accented_characters($string);

    // Este será seu array
    $upload_file_names = array();

    for($i = 0; $i < $len; $i++) {

        $fileSize = $_FILES['userfile']['name'][$i];

        $string = str_replace(' ', '-', $fileSize);

        $new_name = convert_accented_characters($string);

        // Cada linha desta adiciona o nome da linha atual em $upload_file_names
        $upload_file_names[] = $new_name;

    }

    //Configure upload.
    $this->upload->initialize(array(
        "file_name" => array($new_name),
        "upload_path"   => './public/uploads/album/'. $past_date .'/'. $pasta .'/',
        "allowed_types" => "mp3",
        "max_size"  => "2194304000"
    ));

}

Hence in case you have an html like this:

<form>
   <input type="file" name="arquivo[]"> <!--arquivo1.mp3-->
   <input type="file" name="arquivo[]"> <!--arquivo2.mp3-->
   <input type="file" name="arquivo[]"> <!--arquivo3.mp3-->
   <input type="submit">
</form>

In the end $upload_file_names would equal array("arquivo1.mp3", "arquivo2.mp3", "arquivo3.mp3");

0

The right way to do it is by generating a array in conventional forms:

$array = array('valor1', 'valor2', 'valorn');

$array = array('chave1' => 'valor1', 'chave2' => 'valor2', 'chave3' => 'valorn');

$array[] = 'valor1';
$array[] = 'valor2';
$array[] = 'valor3';

$array['chave1'] = 'valor1';
$array['chave2'] = 'valor2';
$array['chave3'] = 'valor3';

But you can make one GABIARRA thus:

$nome = '"nome1", "nome2", "nome3", "chave4" => "valor4"';
$array = eval('return array('.$nome.');');

var_dump($array);

The function Eval() executes the string given in the parameter as PHP code. More details in PHP documentation.

Return:

array(4) {
  [0]=>
  string(5) "nome1"
  [1]=>
  string(5) "nome2"
  [2]=>
  string(5) "nome3"
  ["chave4"]=>
  string(6) "valor4"
}

0

You can generate the array the way you showed it yourself:

$meuArray = array($nome1, $nome2, $nome3);

Another way to set the values, which can also be used in a looping is like this:

$nome1 = "nome 1";
$nome2 = "nome 2";
$nome3 = "nome 3";

$meuArray = array();
$meuArray[] = $nome1;
$meuArray[] = $nome2;
$meuArray[] = $nome3;

echo $meuArray[0]; // Resultado: "nome 1"

Browser other questions tagged

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