CI 묻고 답하기

제목 ci 파일 업로드 시 파일 명 업로드 관련 문의
카테고리 CI 2, 3
글쓴이 개미개발자 작성시각 2021/04/05 18:23:57
댓글 : 6 추천 : 0 스크랩 : 0 조회수 : 8974   RSS

ci를 사용해서 간단한 파일 및 텍스트 업로드를 구현하려고 하는데 

파일이 서버상에 잘 올라가긴 하는데 db상에 올라가질 않네요 ㅠㅠ

시작한 지 얼마 안돼서 너무 어려움을 겪고 있습니다 도움 좀 부탁드릴게요!

Controller

function add(){
    $this->_head();

    $this->load->library('form_validation');

    $this->form_validation->set_rules('title', '제목', 'required');
    $this->form_validation->set_rules('description', '본문', 'required');
    $this->form_validation->set_rules('main_text', '메인 텍스트', 'required');
    $this->form_validation->set_rules('sub_text', '서브 텍스트', 'required');

    if($this->form_validation->run () == FALSE){
        $this->load->view('add');
    }
    else{
        $topic_id = $this->topic_model->add($this->input->post('title'), $this->input->post('description'),
            $this->input->post('main_text'), $this->input->post('sub_text'), $this->input->post('photo'));


        $config['upload_path'] = './static/user';

        $config['allowed_types'] = 'gif|jpg|png';

        $config['max_size'] = '10000M';

        $config['max_width']  = '2048';

        $config['max_height']  = '1096';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if(!$this->upload->do_upload('photo')){
            $error = array('error'=> $this->display_errors());
        } else {
            $data = array('upload_data' => $this->upload->data());
            $filename = $data['file_name'];
            $url = '/static/user/'.$filename;
        }
       
        $this->load->helper('url');
        header('Location: /ant/get/'.$topic_id);
    }
}

 

Model 

function add($title, $description, $main_text, $sub_text, $photo){
    $this->db->set('created', 'NOW()', false);
    $this->db->insert('topic', array(
        'title'=>$title,
        'description'=>$description,
        'main_text'=>$main_text,
        'sub_text'=>$sub_text,
        'photo'=>$photo
    ));
    return $this->db->insert_id();
}

 

view

<div class="form col-6">
    <form action="/ant/add" method="post" enctype="multipart/form-data">
        <?php echo validation_errors(); ?>
        <input type="text" name="title" placeholder="제목" required />
        <textarea name="description" placeholder="본문" required rows="15"></textarea>
        <input type="text" name="main_text" placeholder="메인 텍스트" required />
        <input type="text" name="sub_text" placeholder="서브 텍스트" required />
        <input type="file" name="photo" />
        <button type="submit" class="btn btn-success">게시글 추가</button>
    </form>
</div>

 

 다음글 엔티티 사용법 문제? (5)
 이전글 같은 소스인데 다른 환경에서 500에러 나는 문제 (6)

댓글

프레드윤 / 2021/04/05 18:38:42 / 추천 0

저건 ci3입니다 ci 4는 다르게 사용합니다. 

http://ci4doc.cikorea.net/libraries/uploaded_files.html

 

저같은 경우는 재활용해서 라이브러리에 추가해서 씁니다.

변종원(웅파) / 2021/04/05 18:40:40 / 추천 0
그리고 모델을 호출한 곳이 없습니다. 업로드 성공일 경우 모델의 add()함수를 호출해야 db에 insert 됩니다.
개미개발자 / 2021/04/05 22:16:59 / 추천 0
모델 호출을 해줬는데 어떻게 수정하면 될까요 ?ㅠㅠ
변종원(웅파) / 2021/04/06 09:29:08 / 추천 0
개미개발자/ 모델을 호출했고 데이터를 전달했으면 insert가 작동합니다. 어떤게 더 궁금한건가요?
개미개발자 / 2021/04/06 09:45:45 / 추천 0

파일명을 전달하기 위해 컨트롤러에서 

 $topic_id = $this->topic_model->add($this->input->post('title'), $this->input->post('description'),
            $this->input->post('main_text'), $this->input->post('sub_text'), $this->input->post('photo'));

이 부분을 실행서 파일 명을 post('photo')로 전달하고 싶었는데

function add($title, $description, $main_text, $sub_text, $photo){
    $this->db->set('created', 'NOW()', false);
    $this->db->insert('topic', array(
        'title'=>$title,
        'description'=>$description,
        'main_text'=>$main_text,
        'sub_text'=>$sub_text,
        'photo'=>$photo
    ));
    return $this->db->insert_id();
}

파일명을 전달하기 위해 어떻게 수정해야 할지가 궁금합니다 ㅠ

변종원(웅파) / 2021/04/06 10:16:21 / 추천 0

개미개발자/ 파일업로드 매뉴얼 data 부분 참고하세요. http://www.ciboard.co.kr/user_guide/kr/libraries/file_uploading.html#class-reference

ci의 내부 라이브러리, 함수 등등은 매뉴얼에 잘 설명되어 있습니다. 작업전에 잘 읽어보시고 작업하시는 것을 추천합니다.