控制器:
class gallery extends CI_Controller {
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload'))
{
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view('gallery',$data);
}
} 型号:
<?php
class gallery_model extends CI_Model {
var $gallery_path;
var $gallery_path_url;
function gallery_model()
{
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload()
{
$config = array(
'allowed_types' => 'jpeg|jpg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path. '/thumbs',
'maintain_ration' => TRUE,
'width' => 150,
'height' =>150
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
查看:
<!DOCTYPE html>
<html>
<head>
<title>My gallery</title>
<meta charset="UTF-8"/>
</head>
<body>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload','Upload');
echo form_close();
?>
</div>
</body>
</html>
我想做类似相册的事情。在上传表单中,我将图片文件夹和拇指上的照片上传到拇指文件夹。我想要做的是将图片的标题存储到数据库中并从那里检索,我不知道如何存储图片标题。
答案 0 :(得分:0)
我会在图片标题的表单中添加一个文本字段,从图库模型中将其传递给do_upload方法,或者单独添加到新的图像数据库模型中(以连接和查询数据库)。
我正在写这篇文章并且未经测试。对流程提出一些建议,确保清理输入。 Codeigniter在这方面做得很好,但分享这些建议是很好的编码。此外,您应该喜欢带有标题的图像。我也许也会传递到数据库,图像的dir路径和/或它的拇指路径。它还使您可以更轻松地从库中调用图像,因为您已经在查询数据库中的元数据。类似于foreach($img in$result){ echo $img['title']." - ".echo $img['path'];}
生成“我的标题 - http://example.com/images/thumb/1234.jpg”
..
echo form_input('imgTitle', 'Image Title');
..
..
if($this->input->post('upload'))
{
$data['imgTitle'] = $this->input->post('imgTitle'); //Assigns the post value to the data array so you can pass it via $data to your views
$this->gallery_model->do_upload($data['imgTitle']);
}
..
..
function do_upload($imgTitle=null) //passes a variable as NULL unless specified
{
..
$DB1 = $this->load->database(); //specify db connection/name
$data = array(
'title' => $imgTitle, //passed from controller
'name' => 'My Name');
$this->db->insert('mytable', $data);
..
}