Imagini in CodeIgniter&MySQL
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CodeIgniter2/
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
/config/database.php
db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'images',
'dbdriver' => 'mysqli',
'dbprefix' => '',
………………………………………..
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');
}
public function index()
{
$this->load->helper('url');
$this->data['imgs'] = $this->MainModel->getImages();
$this->load->view('view', $this->data);
}
public function upload()
{
$this->load->helper('form');
$this->load->view('upload');
}
public function save()
{
$url=$this->do_upload();
$title=$_POST["title"];
$this->MainModel->save($title, $url);
redirect('MainController/index');
}
private function do_upload()
{
$type=explode('.',$_FILES["poza"]["name"]);
$type=$type[count($type)-1];
$url="./images/".uniqid(rand()).'.'.$type;
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 "";
}
}
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');
}
public function getImages()
{
$this->db->select('title,image')->from('images');
$query = $this->db->get();
return $query->result();
}
}
views/upload.php
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php echo form_open_multipart('MainController/save'); ?>
<table class="table">
<tr>
<td>Titlu</td>
<td><?php echo form_input('title');?></td>
</tr>
<tr>
<td>Imagine</td>
<td><?php echo form_upload('poza');?></td>
</tr>
<tr>
<td></td>
<td><?php echo form_submit('submit','Save','class="btn btn-primary"')?></td>
</tr>
</table>
</body>
</html>
views/view.php
<!DOCTYPE html>
<head>
</head>
<body>
<table>
<tr>
<td><strong>Nume</strong></td>
<td><strong>Imagine</strong></td>
</tr>
<?php foreach($imgs as $var){?>
<tr>
<td><?php echo $var->title;?></td>
<td><img src="<?php echo base_url($var->image);?>" width="100" height="100"></td>
</tr>
<?php }?>
</table
<br><br>
<?php echo anchor(array('MainController/upload/'),'Upload another image'); ?>
</body>
</html>
• Creati directorul images:
application/config/routes.php
$route['default_controller'] =
'MainController/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
application/config/autoload.php
$autoload['libraries'] = array('database');
$autoload['helper'] = array('form‘,’url’);

19. CodeIgniter imagini in mysql