CI 묻고 답하기

제목 form_open_multipart 한글 깨지는 현상
카테고리 CI 2, 3
글쓴이 FALLIN 작성시각 2016/11/29 21:00:51
댓글 : 2 추천 : 0 스크랩 : 0 조회수 : 20577   RSS

안녕하세요,

코드이그나이터 프로젝트를 하는 과정에서

해결되지 않는 문제가 있어서 이렇게 글을 올리게 되었습니다.

 

form_open_multipart를 이용해서 이미지와 텍스트를 업로드하는데

텍스트에 한글이 포함되어 있는 경우에 깨지는 현상(ex "테스트" => "테스í")이 발생합니다.

 

포럼이나 구글링을 통해서 많은 글들을 확인해봤지만,

이렇게 몇 일이 지나간 건지 모르겠습니다.

저와 같은 문제를 겪은 분들이 계신다면 조언 부탁드립니다.

 

아래에 다소 많은 참고 내용을 쓰게 되었는데 스압 죄송합니다ㅠ

 


1. 환경

AWS EC2 Ubuntu 14.04 LTS입니다.

코드이그나이터는 3.1.0 버전입니다.


2. 설정

*config.php에 charset 설정과,

$config['language'] = 'korean';

$config['charset'] = 'UTF-8';

 

*php.ini의 charset 설정도 아래와 같이 진행하였습니다. 

[PHP]

default_charset = "UTF-8"

[iconv]

;iconv.input_encoding = ISO-8859-1

;iconv.internal_encoding = ISO-8859-1

;iconv.output_encoding = ISO-8859-1

iconv.input_encoding = UTF-8

iconv.internal_encoding = UTF-8

iconv.output_encoding = UTF-8

[mbstring]

mbstring.language = UTF-8

mbstring.internal_encoding = UTF-8

mbstring.http_input = auto //?

mbstring.http_output = UTF-8


3. 컨트롤러

function upload(){
       
        $this->load->library('form_validation');

        $this->form_validation->set_rules('title','제목','required');

        if($this->form_validation->run() == TRUE){
            $config = array(
                'upload_path'   => './include/upload',
                'allowed_types' => 'gif|jpg|jpeg|png',
                'encrypt_name'  => TRUE
            );

            $this->load->library('upload',$config);
            $this->upload->initialize($config);
            if(!$this->upload->do_upload('userfile')){
                $data['error'] = $this->upload->display_errors();
                alert('파일 업로드에 실패했습니다','/');
            }else{
                $upload_data = $this->upload->data();
            }

            $upload_data['title']  = $this->input->post('title', true); //한글 깨짐
            ...
}

4. HTML

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	</head>
	<body>
 		<form 
			action="./upload" 
			class="form-horizontal" 
			name="form" 
			id="upload" 
			enctype="multipart/form-data" 
			method="post" 
			accept-charset="utf-8">

			<input type="file" id='file1' name='userfile' style='display: none;'>
			<input class="title" type="text" name="title" value="" />
	</body>
</html>

5. 반쪽짜리 해결...

아래 링크와 같이 form_helper.php form_open_multipart 함수 내에 코드를 아래와 같이 수정하는 경우

텍스트가 한글이 포함되어도 정상적으로 나오는 반면 이미지를 선택하지 않았다는 메세지(you did not select a file to upload)가 나옵니다...

    function form_open_multipart($action = '', $attributes = array(), $hidden = array())
    {
        if (is_string($attributes))
        {
            //실제
            //$attributes .= ' enctype="multipart/form-data"';
            //수정
            $attributes .= ' enctype="multipart/form-data;charset=utf-8;"';
        }
        else
        {
           //실제
           //$attributes['enctype'] = 'multipart/form-data';
           //수정 
           $attributes['enctype'] = 'multipart/form-data;charset=utf-8';
        }

        return form_open($action, $attributes, $hidden);
    }

https://expressionengine.com/forums/archive/topic/224231/post-encoding-problem#1029834


 

읽어주셔서 감사합니다.

 다음글 codeigniter 프로젝트에서 특정 경로만 접근이 ... (7)
 이전글 codeigniter 설치 후 loclahost 에러... (8)

댓글

FALLIN / 2016/11/29 21:30:05 / 추천 0

자답입니다

(질문을 올리는 것만으로 영감을 주는군요ㅠㅠ)

 

php.ini 

[mbstring]

mbstring.http_input = UTF-8

auto에서 다음과 같이 바꾸니 잘 되는군요...

 

감사합니다

변종원(웅파) / 2016/11/30 09:47:41 / 추천 0
mbstring 옵션은 이유가 있어서 활성화 하신건가요?