CI 코드

제목 Upload 라이브러리 수정본
글쓴이 sisco 작성시각 2010/07/28 19:20:45
댓글 : 0 추천 : 0 스크랩 : 0 조회수 : 16622   RSS
Upload 라이브러리 수정하였습니다. 최신 패치 적용버전

1. 업로드 경로 지정했을때 경로가 없으면 무조건 error 
      => 새로운 함수 추가로 경로가 없을시 폴더 생성 및 index.html 파일 생성
           function make_dirs($path, $last_is_file=false);
$this->make_dirs($this->upload_path); //새로추가

// Is the upload path valid?
if ( ! $this->validate_upload_path())
{
	// errors will already be set by validate_upload_path() so just return FALSE
	return FALSE;
}

2. 파일명 처리 조건 순서 변경
     파일명 클린 > 길이체크 > 공백제거(파일명 강제지정 이름도 공백제거) > encrypt화 > 파일명 강제지정
     한글파일일경우 encrypt 안시키면 제대로 업로드 되더라도 파일명이 깨지는 경우 해결.
     파일명 강제지정은 어떤 경우라도 같은 이름이 있으면 over write

3. encrypt 기본값 FALSE에서 TRUE로 변경

4. Form_validation 과의 연동
    error_count();   파일 업로드시 에러 발생한 경우.. 오류 갯수를 불러줌
    set_errors($msg) 함수 끝에 $this->error_validation($msg); 추가
function error_count(){
	return count($this->error_msg);
}

function error_validation($message){
	$CI =& get_instance();
	if(!isset($CI->form_validation))
		return;
	$CI->form_validation->_field_data[$this->field]['error'] = $message;
	if ( ! isset($CI->form_validation->_error_array[$this->field]))
	{
		$label = $CI->form_validation->_field_data[$this->field]['label'];
		$CI->form_validation->_error_array[$this->field] = $label." : ". $message;
	}
}


function set_error($msg)
{
	$CI =& get_instance();
	$CI->lang->load('upload');
	if (is_array($msg))
	{
		foreach ($msg as $val)
		{
			$msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
			$this->error_msg[] = $msg;
			log_message('error', $msg);
		}
	}
	else
	{
		$msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
		$this->error_msg[] = $msg;
		log_message('error', $msg);
	}
	$this->error_validation($msg);  //추가내용

}
function error_validation($message) 함수 추가 => validation이 활성화 되었을때(로드 되었을때) 에러를 입력
    에러호출은 기존과 마찬가지로  validation_errors()로 동일


메세지 기준은 $lang 파일에 의해 결정됩니다. 한글판 랭귀지 파일을 가져다 쓰심이 좋을듯 합니다.

연계사항 : validation의 rules 에 의해 필드명과 label명이 연계됩니다. 
                    필드명 : 에러가 발생했을시 validation의 field명을 찾아 에러 입력
                    레이블 : 에러발생시 field명의 label값을 가져옴

ex) 파일 업로드
$file_config = array(
	'upload_path' => APPPATH . 'data',
	'file_name' => $this->input->post('userid').".gif",
	'allowed_types' => 'gif',
	'max_size' => 5000,
	'max_width' => 200,
	'max_height' => 100);

$this->load->library('upload', $file_config);
$this->upload->do_upload('user_icon');
if($this->upload->error_count() > 0)
	폼화면으로 이동
 
ex) rules 설정
$this->form_validation->set_rules('user_icon','회원아이콘','trim');

ex) html 파일
<?php echo validation_errors(); ?
ex) 에러 출력사항
회원아이콘 : 업로드하려는 파일 유형은 허용되지 않습니다.
첨부파일 MY_Upload.zip (5.9 KB)
 다음글 CI 1.7.2 한글 언어팩 1.1 (1)
 이전글 iScaffold (4)

댓글

없음