| 제목 | CI 3.1.1 이후 버전에서 파일 업로드 문제 시 php.ini에 php_fileinfo를 확장해주세요. | ||
|---|---|---|---|
| 글쓴이 | PENG | 작성시각 | 2016/12/16 16:31:48 | 
| 
                         | 
                |||
| 
                         안녕하세요, CI를 2013년부터 사용해오면서 포럼 글을 많이 봤지만 회원가입을 하고 글을 작성하는건 처음이네요 :) 3.0.x 버전을 계속 사용하다 최근 회사에서 3.1.2 버전으로 프로젝트를 새로 시작하게 됬습니다. 
 특별한 문제 없이 작업을 진행하던 도중 파일 업로드에 문제를 발견하고 글을 작성하게 되었습니다. 
 
if( ! $this->upload->do_upload($field) ) {
    $errmsg = $this->upload->display_errors();
}
else {
    $fileInfo = $this->upload->data();
}
저는 보통 이런식으로 업로드 예외처리를 하고, 에러가 있으면 로그를 남기는 형식으로 진행을 해왔는데, 어떤 에러메시지도 없이 exit()함수를 호출한 것 처럼 프로그램이 멈춰버리는 현상이 있었습니다. 
 이는 3.1.1 버전 이후에 system/libraries/Upload.php 파일 내 코드가 변경되었기 때문인데요. do_xss_clean()함수와 _file_mime_type()함수가 변경되었고, do_upload()함수와 관련있는 _file_mime_type()함수 변경에 발생한 문제였습니다. 
 * 기존코드 
protected function _file_mime_type($file)
{
	// We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
	$regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
	/* Fileinfo extension - most reliable method
	 *
	 * Unfortunately, prior to PHP 5.3 - it's only available as a PECL extension and the
	 * more convenient FILEINFO_MIME_TYPE flag doesn't exist.
	 */
	if (function_exists('finfo_file'))
	{
		$finfo = @finfo_open(FILEINFO_MIME);
		if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
		{
			$mime = @finfo_file($finfo, $file['tmp_name']);
			finfo_close($finfo);
			/* According to the comments section of the PHP manual page,
			 * it is possible that this function returns an empty string
			 * for some files (e.g. if they don't exist in the magic MIME database)
			 */
			if (is_string($mime) && preg_match($regexp, $mime, $matches))
			{
				$this->file_type = $matches[1];
				return;
			}
		}
	}
	...
 *변경된 코드 
protected function _file_mime_type($file)
{
	// We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
	$regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s.+)?$/';
	// Fileinfo extension - most reliable method
	$finfo = @finfo_open(FILEINFO_MIME);
	if (is_resource($finfo)) // It is possible that a FALSE value is returned, if there is no magic MIME database file found on the system
	{
		$mime = @finfo_file($finfo, $file['tmp_name']);
		finfo_close($finfo);
		/* According to the comments section of the PHP manual page,
		 * it is possible that this function returns an empty string
		 * for some files (e.g. if they don't exist in the magic MIME database)
		 */
		if (is_string($mime) && preg_match($regexp, $mime, $matches))
		{
			$this->file_type = $matches[1];
			return;
		}
	}
	...
 크게 바뀐점은 없지만 function_exists()함수를 통해 finfo_file함수가 있는지 예외처리하는 부분이 사라져있습니다. 주석도 변경이 되었고, 당연히 php_fileinfo extension을 사용할거라는 뉘앙스가 느껴집니다. 
 그래서 php.ini를 열어보니 extension에 php_fileinfo가 빠져있었습니다. 이를 추가하는 것으로 해결되었습니다. upload 라이브러리 호출 시 detect_mime을 FALSE로 바꿔주어도 되지만 권장하지는 않으니까요. 
 요약 : 3.1.1버전 이후 버전 파일 업로드 시 php.ini에 php_fileinfo를 extension해야한다.  | 
                |||
| 다음글 | forge + git push를 이용한 배포방법입니다. (4) | ||
| 이전글 | 삼항연산자 $a = $a ?: null; 을 아시나요? (4) | ||
| 
                             
                                변종원(웅파)
                                /
                                2016/12/17 12:05:27 /
                                추천
                                0
                             
                            
                                정보 감사합니다.
                             
                         | 
                    
| 
                             
                                홍구2
                                /
                                2016/12/18 14:19:20 /
                                추천
                                0
                             
                            
                                공유 감사합니다
                             
                         | 
                    
| 
                             
                                한대승(불의회상)
                                /
                                2016/12/21 16:19:46 /
                                추천
                                0
                             
                            포스팅 안해 주셨으면 모르고 헤매고 있었을것 같습니다. 좋은 정보 감사 합니다.  |