CI 묻고 답하기

제목 파일 업로드 시 컨트롤러에서 못 받는 문제
카테고리 CI 2, 3
글쓴이 주말생각 작성시각 2020/11/06 14:20:09
댓글 : 2 추천 : 0 스크랩 : 0 조회수 : 7864   RSS

오전에 파일 업로드에서 질문드렸고 답변을 받아서 해결했습니다.

그런데 게시글 작성 시 파일을 선택하여 첨부하고 글쓰기 버튼을 눌러서 컨트롤러로 가면 파일을 들고 오질 못하더군요.

<?php 

  $attributes = array('method' => 'post', 'id' => 'myform'); 

  echo form_open('Write', $attributes);

?>

<a>제목  :</a>

<input type="text" id="title" name="title" value="" />

<br>

<textarea id="content" name="content" style="height: 150px; width: 260px"></textarea>

<br>

  <input type="file" name="userfile" size="20" />

  <input type="submit" value="글쓰기" />

  <input type="reset" value="취소" />

<?php echo form_close(); ?>

컨트롤러

public function index()

{

   $this->newWrite();

}

 

public function newWrite(){

   $title = $this->input->post("title");

   $content = $this->input->post("content");

   $datestring = date("Y-m-d H:i:s");

   $sql = "INSERT INTO test_info (title, content, dday)

              VALUES (".$this->db->escape($title).", ".$this->db->escape($content).", ".$this->db->escape($datestring).")";

   $this->db->query($sql);

   $this->do_upload();

   redirect("Board");

}

 

//파일 업로드 기능

public function do_upload(){

   $config['upload_path'] = './uploads/';

   $config['allowed_types'] = 'gif|jpg|png';

   $config['max_size'] = '100';

   $config['max_width']  = '1024';

   $config['max_height']  = '768';

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

 

if ( !$this->upload->do_upload() == NULL )

{

    $error = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);

}

else

{

    $data = array('upload_data' => $this->upload->data());

    print_r($data);

    exit;

  }

}

 

생성자에

$this->load->database();

$this->load->helper(['form', 'url']); 는 추가하였습니다. exit;한 지점에 print_r으로 값을 보면

Array ( [upload_data] => Array ( [file_name] => [file_type] => [file_path] => ./uploads/ [full_path] => ./uploads/ [raw_name] => [orig_name] => [client_name] => [file_ext] => [file_size] => [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => ) 로 출력되는 것으로 봐서는 파일 자체 업로드가 안 되는 것 같습니다. 실제 경로의 폴더에도 저장이 안되고요.

업로드 폼만 따로 두었을 때는 업로드가 되었는데 <input type="file" name="userfile" size="20" />를 글쓰기 폼 안에 넣고 나서 안되네요.

이제 막 공부하는 초보 좀 도와주시면 감사하겠습니다.

 다음글 배열값 추출 (3)
 이전글 로그인 로그아웃 시간 저장 (2)

댓글

PureAni / 2020/11/06 14:22:50 / 추천 0
form_open 함수에서는 파일이 업로드 되지 않습니다.
주말생각 / 2020/11/06 14:37:13 / 추천 0

답변 감사합니다.

뷰 페이지의 폼을 form_open_multipart로 바꾸니 되네요. 그런데 파일이 2번 연속으로 업로드 되는데 이건 무엇이 문제인지 알 수 있을 까요? ex) test.jpg를 첨부해서 글쓰기 버튼을 누르면 uploads폴더에 test.jpg와 test2.jpg가 들어가네요.