Загружать несколько изображений с помощью codeigniter

Я использовал некоторый код, который я нашел раньше, чтобы загрузить 1 изображение за раз, и это работает отлично. Я хотел бы использовать один и тот же стиль кода, но для нескольких изображений.

Модель:

function cover_upload($afbeelding) { // path where the picture needs to be uploaded echo $album_path = APPPATH . 'images/covers'; // configuration for the upload $ext = end(explode(".", $_FILES['userfile']['name'])); $config = array( 'file_name' => $afbeelding . '.' . $ext, 'upload_path' => $album_path, 'allowed_types' => 'gif|jpg|jpeg|png', 'max_size' => '5120' ); // load upload library with the configuration $this->load->library('upload', $config); // upload picture with the upload library if (!$this->upload->do_upload()) { echo $this->upload->display_errors(); die(); } // get data array of the uploaded picture $image_data = $this->upload->data(); // configuration for the picture thumbnail resize echo $image_data['full_path']; $config = array( 'source_image' => $image_data['full_path'], 'new_image' => realpath($album_path . '/thumb'), 'maintain_ration' => TRUE, 'width' => 300, 'height' => 300 ); // load image manupulation library with the configuration $this->load->library('image_lib', $config); // resize picture $this->image_lib->resize(); $this->image_lib->clear(); // submit file name of the uploaded picture to save it in the database $bestandsnaam = $image_data['file_name']; return $bestandsnaam; } 

вид (только часть об изображении):

 <div class="form-group"> <label class="col-sm-2 control-label" for="CoverFoto">picture</label> <div class="col-sm-5"> <div class="fileinput fileinput-new" data-provides="fileinput"> <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div> <div> <span class="btn btn-default btn-file"><span class="fileinput-new">select_an_image</span><span class="fileinput-exists">change</span><input type="file" name="userfile"></span> <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput"><?php echo $this->lang->line("remove"); ?></a> </div> </div> </div> </div> 

И контроллер:

 if (!empty($_FILES['userfile']['name'])) { $gamma->CoverFoto = $this->gamma_model->cover_upload(url_title($gamma->Naam, '_', true)); } 

Есть ли способ использовать этот код, чтобы я мог загружать несколько изображений?

Взгляните на этот код, это поможет понять, как обрабатывать несколько изображений

  ##################### # Uploading multiple# # Images # ##################### $files = $_FILES; $count = count($_FILES['uploadfile']['name']); for($i=0; $i<$count; $i++) { $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i]; $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i]; $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i]; $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i]; $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i]; $this->upload->initialize($this->set_upload_options());//function defination below $this->upload->do_upload('uploadfile'); $upload_data = $this->upload->data(); $name_array[] = $upload_data['file_name']; $fileName = $upload_data['file_name']; $images[] = $fileName; } $fileName = $images; 

что происходит в коде?

well $ _FILE —-> это ассоциативный массив элементов, загруженных в текущий скрипт с помощью метода POST. Для дальнейшего просмотра этой LINK

это автоматическая переменная, доступная во всех сценариях сценария

  function set_upload_options() { // upload an image options $config = array(); $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder $config['remove_spaces']=TRUE; $config['encrypt_name'] = TRUE; // for encrypting the name $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '78000'; $config['overwrite'] = FALSE; return $config; } 

и в вашей html-разметке не забывайте:

  1. Имя входа должно быть определено как массив, то есть name = "file []"
  2. Элемент ввода должен иметь несколько = «несколько» или просто несколько

    3. $ this-> load-> библиотека ( 'загрузка'); // для загрузки библиотеки

    4. Обратный вызов, $ this-> upload-> do_upload () выгрузит файл, выбранный в заданном поле, в папку назначения.

    5. И обратный вызов $ this-> upload-> data () возвращает массив данных, относящихся к загруженному файлу, например имя файла, путь, размер и т. Д.