내 컨트롤러에 잘 작동하는 로컬 디렉토리에 이미지를 업로드하는 다음 기능이 있습니다. 나는 이제 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);
}
방금 수정되고 업데이트 된 매개 변수가 전달되었습니다. –
활성 레코드를 사용하는 것처럼 정상적으로 삽입하십시오. 귀하의 사례에는 특별한 것이 없습니다. – Ghost
귀하의 응용 프로그램 인 반면, 개인적인 팁 만 있으면 원하는 곳에 코드를 배치 할 수 있습니다 (모델을 사용할 필요가없는 이벤트)), 업로드 코드를 모델에 배치하는 것이 더 좋을 것입니다. 아마도 다른 방법 일 수 있습니다. 같은 목적으로 다른 업로드 방법을 사용하기로 결정한 경우 업로드 코드를 컨트롤러에 복제해야하기 때문입니다. 그것을 배치하고 모델에서 동일한 코드를 호출하면 중복을 줄일 수 있습니다. –