How to upload images path to database - El Forum - 12-19-2011
[eluser]swgj19[/eluser]
I read a post on this, however I am just learning this. I have completed the ci from scratch day 9 tutorial and know how to upload images to a directory in my root outside of applications folder. That was easy going.
Now I would like to learn how to upload the image path to my database fields. I know that I have to have an array and pass array $config to the upload library and to $this->db->insert('table_name', $data). Something like that. I also no that my table names have to be the same as variables that will be posted. I also know that I would have to get rid of the get images function and make a new get_images_db function. I found this topic thread and tried to implement the code, but I do not understand. I also read the file upload class as well.
I am more than willing to spend hours on this and do all the coding, I just need to be directed in the right direction. Thanks.
My controller:
Code: <?php
class Gallery extends CI_Controller {
function index() { //class is same name as controller name
$this->load->model('Gallery_model'); //Have to load model to execute code on line 9 with do_upload function
if ($this->input->post('upload')) { //post name from form from view
$this->Gallery_model->do_upload(); //Gallery_model will handel upload.
}
$data['images'] = $this->Gallery_model->get_images(); //get images to pass to the view
$this->load->view('gallery_view', $data); //load gallery_view
}
}
My Model:
Code: <?php
class Gallery_model extends CI_Model {
var $gallery_path;
var $gallery_path_url;
function Gallery_model() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images'); //APPPATH is a constant and set to the path of the application folder
$this->gallery_path_url = base_url(). 'images/'; //realpath function in php cleans up messy paths
}
function do_upload() {
$config = array(
'allowed_types' => 'JPEG|jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000 //size in kilobytes
);
$this->load->library('upload', $config); //initialize the library with config parameter
$this->upload->do_upload(); //Performs upload function above, uploads and saves to the path
$image_data = $this->upload->data(); //upload data function
$config = array(
'source_image' => $image_data['full_path'], //source_image is path of the uploaded image. full path is a key of the array
'new_image' => $this->gallery_path . '/thumbs', //set path for new image in thumbs folder
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config); //load image library
$this->image_lib->resize(); //call the resize function
}
function get_images() {
$files = scandir($this->gallery_path); //scandir is php function, pass gallery path to $files
$files = array_diff($files, array('.', '..', 'thumbs')); //takes two arrays and calculates difference
$images = array();
foreach ($files as $file) {
$images []= array (
'url' => $this->gallery_path_url . $file, //reference image path and filename
'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file //reference thumbs path and filename
);
}
return $images;
}
}
|