CI 묻고 답하기

제목 반복문으로 db 입력 질문드립니다.
글쓴이 해행행 작성시각 2016/02/04 11:17:02
댓글 : 6 추천 : 1 스크랩 : 1 조회수 : 14793   RSS

게시판 파일 업로드 3개를 따로따로 해서 3개를 업로드할려고합니다.

 

controller 단에서 for문으로 model 단으로넘겨서 model에서 받은값을 db에 넣을려고합니다

controller 단에서 코드는 저렇습니다 

//게시물쓰기 + 이미지 업로드
	/* type=file로 프론트 단에서 보낸파일들은 $_FILES[] 배열안에 각자의 name을 가지고 저장되어 있다.  */
	function write() {
		if ($_POST) {
			//쓰기
			$title = $this->input->post ( 'title', TRUE );
			$content = $this->input->post ( 'content', TRUE );
			$b_id = $this->session->userdata('id');
			$cpt = count($_FILES['userfile']['name']);
			
			$uploaded_files = $_FILES;
			$uploaded_file_count = count($_FILES['userfile']['name']);
			
			$this->bobom->insert($title, $content, $b_id);
			
			for($i=0; $i<$uploaded_file_count; $i++) {
				if($uploaded_files['userfile']['name'][$i] == null) continue;
					
				unset($_FILES);
				
				$_FILES['userfile']['name']	 = $uploaded_files['userfile']['name'][$i];
				$_FILES['userfile']['type']	 = $uploaded_files['userfile']['type'][$i];
				$_FILES['userfile']['tmp_name'] = $uploaded_files['userfile']['tmp_name'][$i];
				$_FILES['userfile']['error']	 = $uploaded_files['userfile']['error'][$i];
				$_FILES['userfile']['size']	 = $uploaded_files['userfile']['size'][$i];
				
				$config = array (
					'upload_path' => 'uploads/',				//저장할경로 => "uploads/" 폴더 (기본경로는 index.php 위치)
					'allowed_types' => 'gif|jpg|png',		//파일 타입
					'encrypt_name' => TRUE,				//한글명 가능하게
					'max_size' => '10240'						//최대파일 크기. kb 단위
							);
				
				$image_data= $this->upload->data();
				
				$file_path = $image_data['file_path'];
				$file_name = $image_data['file_name'];
				$orig_name = $image_data['orig_name'];
				
				$this->bobom->insert($file_path, $file_name, $orig_name);
				
				$this->upload->initialize($config);
				if( ! $this->upload->do_upload('userfile')) {
					echo $this->upload->display_errors();
				}
			}
			
			$this->bobom->insert($title, $content, $b_id);
					
			redirect('/');
			
			} else {
				$this->load->view ( 'bobow' );
			}
	}

 

$title, $content, $b_id 를가지고 포문돌기전에 미리 테이블 생성을 해두고 

 

$file_path, $file_name, $orig_name을 포문이돌때마다 model로 넘겨서 입력을 해야될꺼같아

 

그렇게 코드를 짯는데... model에서 값을찍어도 $file_path, $file_name, $orig_name는 찍히질 않네요.. 제가 

 

var_dump 로 값을 찍었는데 잘못찍은건가요? 아니면 다른 방법을 써야할까요.. 

 

매번 새로운 db쿼리를 짤려고하면 막막하네요...

 

 

 

아 그리고 저코드는 막검색하다가 여기저기참조해서 짠겁니다.

 다음글 크론탭 질문 드립니다. (2)
 이전글 페이지네이션 다시 질문합니다. 꼭 좀 도와주세요

댓글

한대승(불의회상) / 2016/02/04 12:21:47 / 추천 0

컨트로러에서 $image_data 의 값을 출력해 보세요.

해행행 / 2016/02/04 13:09:03 / 추천 0
array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(8) "uploads/" ["full_path"]=> string(8) "uploads/" ["raw_name"]=> string(0) "" ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" } 

반복문 안, 밖에서 이런식으로 뜹니다.

그런데 모델쪽에서 찍으면 null 값이뜨네요

한대승(불의회상) / 2016/02/04 13:15:49 / 추천 0

반복문안의 데이터도 업로드된 데이타가 없는것 같군요.

모델쪽 에서 $file_path 로 데이터를 받고 있나요?

해행행 / 2016/02/04 13:26:23 / 추천 0

네 file_path로 넘겻을시 하나만 넘어가고  $image_data 로도 값을 넘겨서 받아봐도 넘어가질않습니다.

변종원(웅파) / 2016/02/04 13:31:43 / 추천 0

this->upload->do_upload('userfile') 를 먼저 해야

$image_data= $this->upload->data(); 이 변수에 값이 담깁니다.

 

소스를 보면 순서가 바뀌어 있죠. 당연히 빈값입니다.

해행행 / 2016/02/04 13:40:51 / 추천 0

아아....... 감사합니다 정말 멍청한짓하고있었네요... 값을안보내놓고 받을려하고있었다니..

 

 function write() {
  if ($_POST) {
   //쓰기
   $title = $this->input->post ( 'title', TRUE );
   $content = $this->input->post ( 'content', TRUE );
   $b_id = $this->session->userdata('id');
   $cpt = count($_FILES['userfile']['name']);
   
   $uploaded_files = $_FILES;
   $uploaded_file_count = count($_FILES['userfile']['name']);
   $image_data = array();
   
   for($i=0; $i<$uploaded_file_count; $i++) {
    if($uploaded_files['userfile']['name'][$i] == null) continue;
     
    unset($_FILES);
    
    $_FILES['userfile']['name']  = $uploaded_files['userfile']['name'][$i];
    $_FILES['userfile']['type']  = $uploaded_files['userfile']['type'][$i];
    $_FILES['userfile']['tmp_name'] = $uploaded_files['userfile']['tmp_name'][$i];
    $_FILES['userfile']['error']  = $uploaded_files['userfile']['error'][$i];
    $_FILES['userfile']['size']  = $uploaded_files['userfile']['size'][$i];
    
    $config = array (
     'upload_path' => 'uploads/',    //저장할경로 => "uploads/" 폴더 (기본경로는 index.php 위치)
     'allowed_types' => 'gif|jpg|png',  //파일 타입
     'encrypt_name' => TRUE,    //한글명 가능하게
     'max_size' => '10240'      //최대파일 크기. kb 단위
       );
    $this->upload->initialize($config);
    $this->upload->do_upload('userfile');
    $image_data = $this->upload->data();
    
    
    $this->bobom->insert2($image_data);
    
   }
   $this->bobom->insert($title, $content, $b_id);
     
   redirect('/');
   
   } else {
    $this->load->view ( 'bobow' );
   }
 }