WEB언어/CodeIgniter

CodeIgniter Multiple file 업로드 하기

saltdoll 2018. 3. 9. 02:55
반응형

코드이그나이터 Multiple file 업로드

다량의 File upload 하기

멀티 파일 업로드 <input type="file" name="upl_files[]"  multiple /> 할 경우

<?php
  /*
  * Code above omitted purposely
  * In your HTML form, your input[type=file] must be named *userfile[]*
  */
   
  /*
  * Uploads multiple files creating a queue to fake multiple upload calls to
  * $_FILE
  */
  public function multiple_upload()
  {
  $this->load->library('upload');
   
  $number_of_files_uploaded = count($_FILES['upl_files']['name']);
   
  // Faking upload calls to $_FILE
  for ($i = 0; $i < $number_of_files_uploaded; $i++) :
  $_FILES['userfile']['name'] = $_FILES['upl_files']['name'][$i];
  $_FILES['userfile']['type'] = $_FILES['upl_files']['type'][$i];
  $_FILES['userfile']['tmp_name'] = $_FILES['upl_files']['tmp_name'][$i];
  $_FILES['userfile']['error'] = $_FILES['upl_files']['error'][$i];
  $_FILES['userfile']['size'] = $_FILES['upl_files']['size'][$i];
   
  $config = array(
  'file_name' => <your ouw function to generate random names>,
  'allowed_types' => 'jpg|jpeg|png|gif',
  'max_size' => 3000,
  'overwrite' => FALSE,
   
  /* real path to upload folder ALWAYS */
  'upload_path'
  => $_SERVER['DOCUMENT_ROOT'] . '/path/to/upload/folder'
  );
   
  $this->upload->initialize($config);
   
  if ( ! $this->upload->do_upload()) :
  $error = array('error' => $this->upload->display_errors());
  $this->load->view('upload_form', $error);
  else :
  $final_files_data[] = $this->upload->data();
  // Continue processing the uploaded data
  endif;
  endfor;
  }
  ?>

 

출처:  https://gist.github.com/zitoloco/1558423

 

 

 

참고: 

(한글) 파일 업로드 Fiel Uploading Class http://www.ciboard.co.kr/user_guide/kr/libraries/file_uploading.html

(한글) 파일 업로드 File Uploading Class (2.1.0)  http://codeigniter-kr.org/user_guide_2.1.0/libraries/file_uploading.html

 

I change upload method with images[] according to @Denmark.

    private function upload_files($path, $title, $files)
    {
        $config = array(
            'upload_path'   => $path,
            'allowed_types' => 'jpg|gif|png',
            'overwrite'     => 1,                       
        );

        $this->load->library('upload', $config);

        $images = array();

        foreach ($files['name'] as $key => $image) {
            $_FILES['images[]']['name']= $files['name'][$key];
            $_FILES['images[]']['type']= $files['type'][$key];
            $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
            $_FILES['images[]']['error']= $files['error'][$key];
            $_FILES['images[]']['size']= $files['size'][$key];

            $fileName = $title .'_'. $image;

            $images[] = $fileName;

            $config['file_name'] = $fileName;

            $this->upload->initialize($config);

            if ($this->upload->do_upload('images[]')) {
                $this->upload->data();
            } else {
                return false;
            }
        }

        return $images;
    }

출처: https://stackoverflow.com/questions/20113832/multiple-files-upload-in-codeigniter

 

 


You can upload any number of files

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $key => $value) {

    if (!empty($value['tmp_name']) && $value['size'] > 0) {

        if (!$this->upload->do_upload($key)) {

            $errors = $this->upload->display_errors();
            flashMsg($errors);

        } else {
            // Code After Files Upload Success GOES HERE
        }
    }
}

And try using HTML like this:

<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />

 

출처: https://stackoverflow.com/questions/9276756/codeigniter-multiple-file-upload

 

반응형
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)