9. MainController.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MainController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('MainModel','f');
}
10. public function index()
{
$this->load->view('view');
}
public function view($id=NULL){
$row=$this->f->getImage($id);
$data['r']=$row;
$this->load->view('view_single',$data);
}
public function upload()
{
$this->load->helper('form');
$this->load->view('upload');
}
11. public function save()
{
$url=$this->do_upload();
$title=$_POST["title"];
$this->f->save($title, $url);
header('location:http://localhost/CodeIgniter2/index.php');
}
private function do_upload()
{
$type=explode('.',$_FILES["poza"]["name"]);
$type=$type[count($type)-1];
$url="./images/".$_FILES["poza"]["name"];
if(in_array($type,array("jpg","jpeg","gif","png")))
if(is_uploaded_file($_FILES["poza"]["tmp_name"]))
if(move_uploaded_file($_FILES["poza"]["tmp_name"],
$url))
return $url;
return "";
}
13. public function update(){
$id=$this->input->post('id');
//create array with input data
$data=array(
'title'=>$this->input->post('title'),
'image'=>"./images/".$_FILES["poza"]["name"]
);
$title=$this->input->post('title');
$image="./images/".$_FILES["poza"]["name"];
move_uploaded_file($_FILES["poza"]["tmp_name"], $image);
//update data
// $this->f->update($title,$image,$id);
$this->db->where('id',$id);
$this->db->update('images',$data);
//redirect
header('location:http://localhost/CodeIgniter2/index.php');
}
}
14. MainModel.php
<?php
class MainModel extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function save($title,$url)
{
$this->db->set('title',$title);
$this->db->set('image',$url);
$this->db->insert('images');
}