CI 묻고 답하기

제목 쇼핑몰에서 파일 업로드 할떄...
글쓴이 람이 작성시각 2014/10/20 09:25:26
댓글 : 5 추천 : 0 스크랩 : 0 조회수 : 15890   RSS
<form action="file-upload.php" method="post" enctype="multipart/form-data">
  이 파일들을 전송합니다:<br />
  <input name="userfile[]" type="file" /><br />
  <input name="userfile[]" type="file" /><br />
  <input type="submit" value="파일 전송" />
</form>

이런식에 대해서는 많은 자료가 있는데...

<form action="file-upload.php" method="post" enctype="multipart/form-data">
  이 파일들을 전송합니다:<br />
  <input name="userfile1" type="file" /><br />
  <input name="userfile2" type="file" /><br />
  <input type="submit" value="파일 전송" />
</form>

이런식으로 파일을 처리하는거는 잘 못 찾겠어요..
1. 컨트롤러단만 예시가 있으면 이해를 할 수 있을 것 같은데 ?
2. 중복된 파일을 처리하는건 어떻게 하나요 ?
3. 저장될때 파일명을 바꿔 저장하는건 어떻게 하나요 ?
4. 이미지의 사이즈를 비율적으로 강제로 줄이려면 (썸네일 만들고자) 어떻게 하나요 ?
 다음글 모델 $this->db 처리 인식문제. (7)
 이전글 oauth 구현관련 질문입니다. (3)

댓글

수야디벨 / 2014/10/20 09:50:13 / 추천 0
당연한 질문이겠지만 코드이그나이터 사용하시는거죠? (그러니 여기에 질문을 ..^^;)

그렇다면 하나의 파일을 올리고 

$this->upload->initialize($config); 를 이용하셔서 설정값을 초기화 하신후 

다시 파일업로드를 하시면 됩니다.

즉 코드이그나이터 파일업로드를 두번 하는거에요.


http://codeigniter-kr.org/user_guide_2.1.0/libraries/file_uploading.html

참고해보세요

그리고 이미지 처리는


http://codeigniter-kr.org/user_guide_2.1.0/libraries/image_lib.html

참고하시구요
 
람이 / 2014/10/20 10:06:44 / 추천 0
수야디벨님 감사합니다.
아직 링크를 가보지 않았지만 빠른 답변 주셔서 감사합니다.
kaido / 2014/10/20 10:14:54 / 추천 0
저도 사실 이전부터 궁금했던건데...

설정을 초기화 하고 다시 업로드를 한다면

name= 'userfile1 이걸 먼저 처리하고
userfile2 다음에 이걸 처리 한다는 말씀인가요?


userfile1 = 파일있음
userfile2 = 파일없음
userfile3 = 파일있음

이 경우에는 어떻게 구분해서 처리 하면 되나요?

그리고 1 2 3 번 전부 각기 다른 설정을 사용해야 합니다.
 
람이 / 2014/10/20 10:32:18 / 추천 0
<form method="post" action="uploader/go" enctype="multipart/form-data">
  <input type="file" name="image1" /><br />
  <input type="file" name="image2" /><br />
  <input type="file" name="image3" /><br />
  <input type="file" name="image4" /><br />
  <input type="file" name="image5" /><br />
  <input type="submit" name="go" value="Upload!!!" />
</form>

class Uploader extends Controller {
  function go() {
    if(isset($_POST['go'])) {
      /* Create the config for upload library */
      /* (pretty self-explanatory) */
      $config['upload_path'] = './assets/upload/'; /* NB! create this dir! */
      $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
      $config['max_size']  = '0';
      $config['max_width']  = '0';
      $config['max_height']  = '0';
      /* Load the upload library */
      $this->load->library('upload', $config);
 
      /* Create the config for image library */
      /* (pretty self-explanatory) */
      $configThumb = array();
      $configThumb['image_library'] = 'gd2';
      $configThumb['source_image'] = '';
      $configThumb['create_thumb'] = TRUE;
      $configThumb['maintain_ratio'] = TRUE;
      /* Set the height and width or thumbs */
      /* Do not worry - CI is pretty smart in resizing */
      /* It will create the largest thumb that can fit in those dimensions */
      /* Thumbs will be saved in same upload dir but with a _thumb suffix */
      /* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
      $configThumb['width'] = 140;
      $configThumb['height'] = 210;
      /* Load the image library */
      $this->load->library('image_lib');
 
      /* We have 5 files to upload
       * If you want more - change the 6 below as needed
       */
      for($i = 1; $i < 6; $i++) {
        /* Handle the file upload */
        $upload = $this->upload->do_upload('image'.$i);
        /* File failed to upload - continue */
        if($upload === FALSE) continue;
        /* Get the data about the file */
        $data = $this->upload->data();
 
        $uploadedFiles[$i] = $data;
        /* If the file is an image - create a thumbnail */
        if($data['is_image'] == 1) {
          $configThumb['source_image'] = $data['full_path'];
          $this->image_lib->initialize($configThumb);
          $this->image_lib->resize();
        }
      }
    }
    /* And display the form again */
    $this->load->view('upload_form');
  }
}
kaido / 2014/10/20 14:01:15 / 추천 0
$upload = $this->upload->do_upload('image'.$i); 

여기에다 넣고 구분 하는 거였군요. 
이걸 몰라서 여태 각기 다른 업로드 사용시 그냥 하드코딩 형태로 만들어서 사용했었지요.

오늘 득 코드를 해서 살짝 기분 +1