【发布时间】:2014-09-16 07:03:46
【问题描述】:
在我的控制器中,我有以下功能可以将图像上传到本地目录,效果很好。我现在想存储在数据库中。我只需要弄清楚我已经拥有并认为正确的代码应该放在哪里。
我需要插入这个:
$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);
}
【问题讨论】:
-
刚刚编辑和更新的参数通过了。
-
就像使用活动记录一样正常插入它,你的情况没有什么特别的
-
只是个人提示,虽然它是您的应用程序,并且您可以将代码放置在您想要的位置(您不需要使用模型),最好将上传代码放置在模型中同样,也许作为另一种方法。这是因为如果您决定为同一目的使用另一种上传方法,则需要在控制器中复制上传代码。放置它并从模型中调用相同的代码将减少重复。
-
谢谢。那是真实的。但是,这不是我的应用程序,我必须处处处理意大利面条代码。
-
我插入并测试了,但出于某种奇怪的原因,它破坏了我的裁剪功能,这没有任何意义。
标签: php codeigniter