Mysql insertion problem with Multi upload

Asked

Viewed 182 times

1

I have a system that performs multi file upload, I am using the Codeigniter framework that extends to Class Upload CI. The files are being moved correctly to the desired folder, however when entering the database records only one.

Controller:

  if ($this->form_validation->run() == TRUE):
        $upload = $this->protocolo->do_upload('arquivo');
        if (is_array($upload) && $upload['file_name'] != ''):
        $dados['arquivo'] = $upload['file_name'];
        $this->protocolo->do_insert($dados);
    endif;

View:

echo form_label('Arquivo');
echo form_upload(array('name' => 'arquivo[]', 'class' => 'arquivo', 'multiple' => ''), set_value('arquivo'));

Upload model:

 public function do_upload($campo = 'arquivo') {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);
    if ($this->upload->do_multi_upload($campo)):
        return $this->upload->data();
        else:
        return $this->upload->display_errors();
    endif;
}

Data insertion model:

public function do_insert($dados = NULL) {
    if ($dados != NULL):
        $this->db->insert('protocolo', $dados);
    endif;
}

I performed a test, I used a function that the multi upload class offers, called get_multi_upload_data() and she returned me the data correctly:

 if ($this->form_validation->run() == TRUE):
    $upload = $this->protocolo->do_upload('arquivo');
    if (is_array($upload) && $upload['file_name'] != ''):
        echo '<pre>';
            $upload = $this->upload->get_multi_upload_data();
            print_r($upload);
        echo '</pre>';
Array
(
    [0] => Array
        (
            [file_name] => 311cc35c96ca64b7750cdce8e8f548d1.jpg
            [file_type] => image/jpeg
            [file_path] => D:/wamp/www/sistemaProtocolo/uploads/
            [full_path] => D:/wamp/www/sistemaProtocolo/uploads/311cc35c96ca64b7750cdce8e8f548d1.jpg
            [raw_name] => 311cc35c96ca64b7750cdce8e8f548d1
            [orig_name] => Copia.jpg
            [client_name] => Copia.jpg
            [file_ext] => .jpg
            [file_size] => 851.84
            [is_image] => 1
            [image_width] => 1144
            [image_height] => 857
            [image_type] => jpeg
            [image_size_str] => width="1144" height="857"
        )

    [1] => Array
        (
            [file_name] => aa960f3a2566ab709e9cfc8d10a5db99.jpg
            [file_type] => image/jpeg
            [file_path] => D:/wamp/www/sistemaProtocolo/uploads/
            [full_path] => D:/wamp/www/sistemaProtocolo/uploads/aa960f3a2566ab709e9cfc8d10a5db99.jpg
            [raw_name] => aa960f3a2566ab709e9cfc8d10a5db99
            [orig_name] => dsds.jpg
            [client_name] => dsds.jpg
            [file_ext] => .jpg
            [file_size] => 877.92
            [is_image] => 1
            [image_width] => 1029
            [image_height] => 772
            [image_type] => jpeg
            [image_size_str] => width="1029" height="772"
        )

)

The problem is only in the insertion of the BD, it is inserting only one record, someone has idea of what can be?

1 answer

0


See if this code can help you.

<?php           

            // Carrega a library
            $this->load->library('upload');

            // Verifica se tem arquivos no input
            if (!empty($_FILES['files'])) 
            {
                // Conta os arquivos carregados
                $len2 = count($_FILES['files']['name']);

                // Coloca todos arquivos na array
                $nomes = array();

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

                    $nomes[] = $_FILES['files']['name'][$i];

                }

                //Configura upload.
                $this->upload->initialize(array(
                    "file_name" => $nomes,
                    "upload_path"   => "./uploads/",
                    "allowed_types" => "gif|jpg|png|pdf",
                    "encrypt_name"  => TRUE
                ));

                //faz o upload de vários arquivos
                if($this->upload->do_multi_upload("files")) 
                {

                    // Retorna detalhes dos arquivos enviados
                    $upload_info = $this->upload->get_multi_upload_data();  

                    // Inicia o array dos dados que serão inseridos
                    $arr = array();

                    // Varre o array com info. dos arquivos
                    for ($i=0; $i < sizeof($upload_info); $i++) 
                    {

                        // Nome do arquivo em cada elemento
                        $nomearquivo = $_FILES['files']['name'][$i];


                        // Adiciona no array para inserir no banco
                        $arr[] = array(

                            'arquivo' => $nomearquivo

                        );

                    }

                    // Insere tudo no banco de dados
                    $this->db->insert_batch('banco', $arr);
                }
            }
  • It worked perfectly, I used your logic and adapted mine. VLW...

  • You have to choose a reply @Andrewmaxwell

Browser other questions tagged

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