[eluser]bidur[/eluser]
Hi all,
I am trying to upload image into database.I am successful to do so (by referring to different threads in this forum).
But my problem is that I am not able to follow the Model-View-Controller(MVC) way.
I have a working code with a controller and a view.
I am hoping that someone could help me by guiding how can I use model in my code.(probably by separating the codes into different classes)
The View goes as follows:
Code:
<?=form_open_multipart('upload/do_upload'); ?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
The controller goes as follows:
Code:
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->content['error'] = array('error' => $this->upload->display_errors());
$this->load->view('upload/do_upload', $this->content);
}
else {
$data = $this->upload->data();
$this->content['upload_data'] = $data;
$img_name=$data['file_name'];
$image_path="./uploads/".$img_name;
$fh = fopen($image_path, "r");
$data = addslashes(fread($fh, filesize($image_path)));
fclose($fh);
$this->load->database();
$dbdata = array(
'id'=>'',
'description' => $img_name,
'photo'=>$data
);
$this->db->insert('photo', $dbdata);
$image_id = $this->db->insert_id();
redirect('upload');
}
}
Thank You