2014-09-16 3 views
0

내 컨트롤러에 잘 작동하는 로컬 디렉토리에 이미지를 업로드하는 다음 기능이 있습니다. 나는 이제 DB에 저장하고 싶다. 난 이미 가지고있는 코드를 어디에 넣어야 하는지를 생각할 필요가있다.어디서 삽입합니까? db query codeigniter

나는이 삽입해야 전이나 그림이 로컬 디렉토리에 업로드 한 후 하나에

$this->location->store_file_path($file); 

. 어디로 가야합니까?

컨트롤러 :

public function image() { 

    //type must either be type=logo or type=profile_picture 

    if (isset($_FILES['file']) && isset($_POST['type'])) { 

     //Codeigniter upload 
     $config['upload_path'] = 'uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['encrypt_name'] = TRUE; 
     $config['max_size'] = 5120; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 
     $this->upload->initialize($config); 

     if (!$this->upload->do_upload('file')) { 

      $status = 'error'; 
      $msg = $this->upload->display_errors('', ''); 
     } else { 
      $data = $this->upload->data(); 
      //$file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']); 
      if ($data) { 
       $status = "success"; 
       $data['url'] = $config['upload_path'] . $data['file_name']; 
       $msg = $data; 
      } else { 
       unlink($data['full_path']); 
       $status = "error"; 
       $msg = "Something went wrong when saving the file, please try again."; 
      } 


     } 
    } else { 
     $status = "error"; 
     $msg = "Please select a file to upload"; 
    } 
    echo json_encode(array('status' => $status, 'msg' => $msg)); 
} 

가 여기 내 모델 기능입니다 :

public function store_file_path($file) 
{ 

    $this->db->insert('TACTIFY_locationcards', 'name'=>$file); 
} 
+0

방금 ​​수정되고 업데이트 된 매개 변수가 전달되었습니다. –

+0

활성 레코드를 사용하는 것처럼 정상적으로 삽입하십시오. 귀하의 사례에는 특별한 것이 없습니다. – Ghost

+0

귀하의 응용 프로그램 인 반면, 개인적인 팁 만 있으면 원하는 곳에 코드를 배치 할 수 있습니다 (모델을 사용할 필요가없는 이벤트)), 업로드 코드를 모델에 배치하는 것이 더 좋을 것입니다. 아마도 다른 방법 일 수 있습니다. 같은 목적으로 다른 업로드 방법을 사용하기로 결정한 경우 업로드 코드를 컨트롤러에 복제해야하기 때문입니다. 그것을 배치하고 모델에서 동일한 코드를 호출하면 중복을 줄일 수 있습니다. –

답변

0

변화 controler를

$status = "success"; 
+ $file = $config['upload_path'] . $data['file_name']; 
+ $this->location->store_file_path($file); 
$msg = $file; 

에 다음 줄 : 내가 여기에 라인을 넣어 것

  if ($data) { 
      $status = "success"; 
      $data['url'] = $config['upload_path'] . $data['file_name']; 
      $msg = $data; 

: 나는 성공 올바르게 코드를 읽으면

는 점에 모델 변경

public function store_file_path($file) 
{ 
    + $this->db->set('name', $file); 
    + $this->db->insert('TACTIFY_locationcards'); 
    - $this->db->insert('TACTIFY_locationcards', 'name'=>$file); 
} 
0

난 당신이 한 번만 모든 검증이 완료되었으며, 데이터가 파일에 기록 된 데이터베이스에 쓰기 좋습니다.

  if ($data) { 
      $status = "success"; 
      $data['url'] = $config['upload_path'] . $data['file_name']; 
      $msg = $data; 
      $this->location->store_file_path($file);