2010-06-22 6 views
1

두 번째 CI 응용 프로그램을 작성 중입니다. 내 컨트롤러에 간단한 CRUD 메서드를 만들었습니다. 제 의뢰인이 각 기록에 이미지가 포함되도록 요청했습니다. 포럼 및 기타 리소스를 검색했지만 많은 행운이 없었습니다.Codeigniter - 내 CRUD 방법에 이미지를 추가하는 방법?

File Uploading Class를 사용하여 디렉토리에 파일을 업로드 할 수 있지만 문제는 업로드 된 파일을 관련 레코드와 연결하는 방법입니다.

다음은 내 MVC의 관련 부분입니다. 올바른 방향으로 어떤 도움말/요점을 높이 평가할 수 있습니다.

보기 - 관리자/locationEdit.php

<form method="post" action="<?php echo $action; ?>"> 
     <div class="data"> 
     <table> 
      <tr> 
       <td width="30%">ID</td> 
       <td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->id; ?>"/></td> 
       <input type="hidden" name="id" value="<?php echo $this->validation->id; ?>"/> 
      </tr> 
      <tr> 
       <td valign="top">Name<span style="color:red;">*</span></td> 
       <td><input type="text" name="name" class="text" value="<?php echo $this->validation->name; ?>"/> 
       <?php echo $this->validation->name_error; ?></td> 
      </tr> 
      <tr> 
       <td valign="top">Address<span style="color:red;">*</span></td> 
       <td><input type="text" name="address" class="text" value="<?php echo $this->validation->address; ?>"/> 
        <?php echo $this->validation->address_error; ?></td> 
      </tr> 

      <tr> 
       <td>&nbsp;</td> 
       <td><input type="submit" value="Save"/></td> 
      </tr> 
     </table> 
     </div> 
     </form> 

컨트롤러 - 업로드 기능 내에서 location.php

function add(){ 
     // set validation properties 
     $this->_set_fields(); 

     // set common properties 
     $data['title'] = 'Add new location'; 
     $data['message'] = ''; 
     $data['action'] = site_url('admin/location/addLocation'); 
     $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back')); 



      // Write to $title 
     $this->template->write('title', 'Admin - Add New Location!'); 
    // Write to $sidebar 
     $this->template->write_view('content', 'admin/locationEdit', $data); 
     // Render the template 
     $this->template->render(); 

    } 

function addLocation(){ 
     // set common properties 
     $data['title'] = 'Add new location'; 
     $data['action'] = site_url('admin/location/addLocation'); 
     $data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back')); 

     // set validation properties 
     $this->_set_fields(); 
     $this->_set_rules(); 

     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

     $path_to_uploads='./uploads'; 
      $config['upload_path'] = $path_to_uploads; 
      $this->load->library('upload', $config); 

      $upload_data=$this->upload->data(); 
      $file_name=$upload_data['file_name']; 
      $full_file_path = $path_to_uploads.'/'.$file_name; 

     // run validation 
     if ($this->validation->run() == FALSE){ 
      $data['message'] = ''; 
     }else{ 


      // save data 
      $location = array('name' => $this->input->post('name'), 
          'address' => $this->input->post('address'), 
          'image_url' => $full_file_path); 
      $id = $this->locationModel->save($location); 

      // set form input name="id" 
      $this->validation->id = $id; 

      // set user message 
      $data['message'] = '<div class="success">add new location success</div>'; 
     } 

     // Write to $title 
     $this->template->write('title', 'Admin - Add New Location!'); 
    // Write to $sidebar 
     $this->template->write_view('content', 'admin/locationEdit', $data); 
     // Render the template 
     $this->template->render(); 
    } 

답변

2

, 업로드 된 파일의 경로를 얻을 :

$config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '100'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $path_to_uploads='./uploads'; 
    $config['upload_path'] = $path_to_uploads; 
    $this->load->library('upload', $config); 
    //add this 
    $this->upload->initialize($config); 
    if (!$this->upload->do_upload()){ 
     $error = $this->upload->display_errors(); 
     echo "<script>alert($error);</script>"; 
    }else{ 
     $upload_data=$this->upload->data(); 
     $file_name=$upload_data['file_name']; 
     $full_file_path = $path_to_uploads.'/'.$file_name; 
    } 

을 그런 다음 $full_file_path을 호출 한 메소드로 되돌려 db에 삽입하거나 inse 직접 rt.

+0

빠른 응답 stormdrain 주셔서 감사합니다, 그러나, 나는 마지막 장애물에 붙어 있습니다. 레코드를 삽입하면 필드의 내용이 ./uploads/로 읽히고 업로드 폴더를 확인할 때 아무것도 없습니다. 이것은 무엇을 의미합니까? 도움을 주셔서 감사합니다. Dan –

+0

업로드가 실패한 것 같습니다. 위의 질문을 수정하여 업로드를 처리하는 데 사용하는 코드를 포함시킬 수 있습니까? 파일 업로드 클래스를 사용할 때 어떤 이유로 장애가 발생하면 대개 그 이유를 알려줍니다. 업로드 폴더에 대한 권한을 올바르게 설정 했습니까 (예 : chmod 777)? – stormdrain

+0

그것은 Mac에서 로컬 설치, 나는 모든 업로드 폴더에 대한 읽기/쓰기 속성을 변경했습니다. 위에 새 코드를 추가했습니다. 내가 잘못하고있는 것을 볼 수 있습니까? 감사합니다, Dan –

관련 문제