php codeigniter file-upload

php - Carga de múltiples imágenes con CodeIgniter



file-upload (3)

Soy un usuario nuevo para usar el encendedor de código en mi proyecto, me enfrento a un problema al cargar varios archivos, pero el último solo se inserta en todas las imágenes del campo de tres imágenes.

mi controlador es:

function products() { date_default_timezone_set("Asia/Kolkata"); $config[''upload_path''] = ''./resources/images/products/''; $config[''allowed_types''] = ''gif|jpg|png''; $config[''max_size''] = 1000; $config[''max_width''] = 1024; $config[''max_height''] = 768; $this->load->library(''upload'', $config); $this->upload->do_upload(''userfile''); $data = array(''prod_image'' => $this->upload->data(), ''prod_image1'' => $this->upload->data(), ''prod_image2'' => $this->upload->data()); $product_image=$data[''prod_image''][''file_name'']; $product_image1=$data[''prod_image1''][''file_name'']; $product_image2=$data[''prod_image2''][''file_name'']; $data = array( ''name'' => $this->input->post(''pd_name''), ''prod_image'' => $product_image, ''prod_image1'' => $product_image1, ''prod_image2'' => $product_image2, ''created_time'' => date(''Y-m-d H:i:s'')); // insert form data into database $result_set= $this->tbl_products_model->insertUser($data); }

mi punto de vista es:

<input class="form-control" name="pd_name"type="text"/> <input type="file" class="file_upload2" name="userfile"/> //1 <input type="file" class="file_upload2" name="userfile"/> //2 <input type="file" class="file_upload2" name="userfile"/>//3

Por favor, ayuda cómo insertar 3 imágenes.

mi base de datos como

=========================================== id|name|prod_image|prod_image1|prod_image2| =========================================== 1|ard| | | | ============================================


El problema es con la siguiente línea de código:

<input type="file" class="file_upload2" name="userfile"/> //1 <input type="file" class="file_upload2" name="userfile"/> //2 <input type="file" class="file_upload2" name="userfile"/>//3

Estos tres tienen el mismo nombre.

Para resolver esto hay dos formas:

Dar nombre diff a los 3 archivos de tipo de entrada

Haga que un único archivo de tipo de entrada con su selección de archivos múltiples sea verdadero y su nombre debe ser una matriz como:

<input type="file" name="filefield[]" multiple="multiple">

Realice los siguientes cambios e intente nuevamente.


HTML:

<input type="file" name="userfile[]" multiple="multiple">

PHP:

<?php public function products() { $this->load->library(''upload''); $dataInfo = array(); $files = $_FILES; $cpt = count($_FILES[''userfile''][''name'']); for($i=0; $i<$cpt; $i++) { $_FILES[''userfile''][''name'']= $files[''userfile''][''name''][$i]; $_FILES[''userfile''][''type'']= $files[''userfile''][''type''][$i]; $_FILES[''userfile''][''tmp_name'']= $files[''userfile''][''tmp_name''][$i]; $_FILES[''userfile''][''error'']= $files[''userfile''][''error''][$i]; $_FILES[''userfile''][''size'']= $files[''userfile''][''size''][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); $dataInfo[] = $this->upload->data(); } $data = array( ''name'' => $this->input->post(''pd_name''), ''prod_image'' => $dataInfo[0][''file_name''], ''prod_image1'' => $dataInfo[1][''file_name''], ''prod_image2'' => $dataInfo[2][''file_name''], ''created_time'' => date(''Y-m-d H:i:s'') ); $result_set = $this->tbl_products_model->insertUser($data); } private function set_upload_options() { //upload an image options $config = array(); $config[''upload_path''] = ''./resources/images/products/''; $config[''allowed_types''] = ''gif|jpg|png''; $config[''max_size''] = ''0''; $config[''overwrite''] = FALSE; return $config; } ?>


Varios archivos cargan archivos ilimitados

Los nombres de columna de la tabla de base de datos ( profile_images ) son image_name (255, varcher) , Added_datetime (marca de tiempo actual)

Ver

<?php echo validation_errors();?> <?php echo form_open_multipart(''pages/multiple_files'');?> <p><input type="file" multiple="multiple" name="image_name[]" class="form-control" /></p> <input type="submit" class="btn btn-success btn-block"/> </form>

Controlador

public function multiple_files(){ $this->load->library(''upload''); $image = array(); $ImageCount = count($_FILES[''image_name''][''name'']); for($i = 0; $i < $ImageCount; $i++){ $_FILES[''file''][''name''] = $_FILES[''image_name''][''name''][$i]; $_FILES[''file''][''type''] = $_FILES[''image_name''][''type''][$i]; $_FILES[''file''][''tmp_name''] = $_FILES[''image_name''][''tmp_name''][$i]; $_FILES[''file''][''error''] = $_FILES[''image_name''][''error''][$i]; $_FILES[''file''][''size''] = $_FILES[''image_name''][''size''][$i]; // File upload configuration $uploadPath = ''./assets/images/profiles/''; $config[''upload_path''] = $uploadPath; $config[''allowed_types''] = ''jpg|jpeg|png|gif''; // Load and initialize upload library $this->load->library(''upload'', $config); $this->upload->initialize($config); // Upload file to server if($this->upload->do_upload(''file'')){ // Uploaded file data $imageData = $this->upload->data(); $uploadImgData[$i][''image_name''] = $imageData[''file_name'']; } } if(!empty($uploadImgData)){ // Insert files data into the database $this->pages_model->multiple_images($uploadImgData); } }

Modelo

public function multiple_images($image = array()){ return $this->db->insert_batch(''profile_images'',$image); }