개발 Q&A

제목 이미지 첨부가 포함된 수정 페이지에서...
카테고리 PHP
글쓴이 신일 작성시각 2019/10/14 21:25:15
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 8238   RSS

안녕하세요, 궁금한 점이 있어 문의 드립니다. 

아래 코드는 이력서 수정 컨트롤러 인데요.  수정 페이지라서 이력서 사진 첨부가 선택 항목입니다.    

선택 항목이라 사진 첨부가 될수도 있고, 안될수도 있습니다.

사진을 첨부하지 않으면 500 에러가 발생 하는데, 코드 중 [3]번 if문을 제거하면 발생하지 않습니다.

 

하지만, 사진 첨부하는 경우도 있어서 이부분을 제거할 수는 없을 듯합니다. 어떻게 하면 에러를 해결할 수 있을까요?

 

 

public function my_profile_edit()                                                        
{  
	$this->form_validation->set_rules('introduce', '한줄소개', 'required');

	if ( $this->form_validation->run() == TRUE )                                                   
	{  
		//[1] CI upload 라이브러리 기본설정
		$config = array(
			'upload_path' => 'uploads/profile_img/',
			'allowed_types' => 'gif|jpg|png',
			'encrypt_name' => TRUE,             
			'max_size' => '1000'
		);
		//[2] CI upload 라이브러리 호출 
		$this->load->library('upload', $config);

		//[3] $this->upload->do_upload()은 업로드를 수행한다
		if (!$this->upload->do_upload())                //업로드 실패시
		{
			$error_data[] = array('error' => $this->display_errors());    //에러 메세지
            alert($error_data);                                           //원래 페이지로 돌아감
		}
		else                                                              //업로드 성공시
		{
		    //$this->upload->data는 업로드된 파일의 다양한 정보를 갖고 있다
            $upfile_info = $this->upload->data();
            
            //[4] 1단계를 저장한다
		    $data = array(                                                          //db에 요청할 값을 배열로 만듬. db필드명 => 수정값 
			  'profile_img_path' => $upfile_info['file_path'] ,                                        //업로드된 프로필 이미지 저장경로 ex)/path/upload/
			  'profile_img_name' => $upfile_info['file_name'] ,                                        //업로드된 프로필 이미지명 ex)abc.jpg                   
			  'introduce' => $this->input->post('introduce', TRUE) ,                                   //한줄소개          
		    );
            $result = $this->profile_basic_m->my_profile_edit_01_update($data);                        //모델에 update 요청
	        echo json_encode($result); 
	    }
	} 
	else                                                                                               //프로필 등록 1단계 페이지 접속 시 
	{	
        $data['freelancer_detail'] = $this->profile_basic_m->freelancer_detail($pbs_id);               //한줄소개,직종,생년월일,집주소 입력값
	    $this->load->view('html/mypage/freelancer/my_profile/my_profile_edit_01_v', $data); 
	}          
}

 

 다음글 비주얼 스튜디오 코드를 사용 하는데 프로그램 작동이 안... (3)
 이전글 codeigniter를 docker로 구성 시에 ind... (4)

댓글

변종원(웅파) / 2019/10/15 08:58:00 / 추천 0

수정은 전송된 값이 있는지 확인하는게 먼저죠

$_FILES 변수를 먼저 체크하시면 됩니다. 내용이 있으면 3번 실행

신일 / 2019/10/15 23:51:38 / 추천 0

웅파님, 뷰에서 userfile로 값을 넘기는데... 전송된 값이 있는지 체크하기 위해 아래와 같이 하니까 안되네요..

파일은 아래와 같이 일반적인 방식으로는 안되는건지요?

if ( $this->input->post('userfile') ) {  }  

 

 

kaido / 2019/10/16 00:23:34 / 추천 0
//업로드 설정
        $my_config = array(
            'upload_path' => FCPATH . 'upload/favicon/',
            'allowed_types' => 'png',
            'max_size' => '10485760', //10mb
            'max_width' => '1024',    //px
            'max_height' => '768',   //px
            'encrypt_name' => true
        );
		
		//폴더 유무 확인 없으면 생성
		$dirpath = $my_config['upload_path'];		
        if (!is_dir($dirpath)) {
            @mkdir($dirpath, 0777);
		}
        
        if( count(array_keys($_FILES)) > 0)  {
            $file_key = implode(array_keys($_FILES));
        }
        
        $this->CI->load->library('upload', $my_config);
        // Upload init
        $this->CI->upload->initialize($my_config);
        // Do Upload
        if (!$this->CI->upload->do_upload($file_key)) {
			//업로드 실패
            $data = array('error' => $this->CI->upload->display_errors(), 'upload_data' => '');
        } else {
           $data = array('upload_data' => $this->CI->upload->data(), 'error' => '');
			//업로드 성공
        }

print_r($data);

참고해 보시기 바랍니다