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

2

Type I want to generate an array like this.

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

What I have is the string $ name, 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"
            ));
    
asked by anonymous 07.01.2015 / 19:55

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 the way I showed 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; , in this way each new element will be added at the end of the array.

Edit:

Adapting to your code can do this:

$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...
    
07.01.2015 / 20:06
2

So I understand you want to generate an array containing the list of file names that will be sent, if it is, use empty brackets, so it automatically defines the index:

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"
    ));

}

Then 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>

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

    
07.01.2015 / 20:24
0

The right way to do this is to generate an array in conventional ways:

$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 do a GABIARRA like this:

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

var_dump($array);

The eval () function executes the string given in the parameter as PHP code. More details on PHP Documentation .

Return:

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

You can generate the array the way you showed it:

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

Another way to set values, which can also be used in a looping is as follows:

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

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

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