• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display Above Root Image with a controller on CI3-RC2


Some time ago I have found on the elislab forum a code ( image controller ) to display images from outside of the site public directory.
The idea is to keep private images and private files far from public access and unauthorized access. The code can be modify to be use with files too.

I have made some minor modifications on it and it works perfectly so far on CI3-Developer Version and CI3-RC2.

But it have found an unexpected behavior with the image controller and CI3-RC2 Session_files_driver.

What unexpected behavior?

If I access a page on CI3-RC2 the Session_files_driver create 1 session file per-user/per-page request. Thats OK! no problem.

But if I access the same page using the image controller with 12 images on it the CI3-RC2 Session_files_driver will produce about 13 session files almost instantly per-user/per-page request.

That will be a lot of trash on the session folder on a busy site.

So... is that a normal behavior for the Session_files_driver?

Or... It is an image controller overhead of some kind?

Am I missing something?

I appreciated suggestions to improve the image controller to avoid the excess of session files on the system.

Below are the settings and the image controller;

// folder structure



// application/config/config.php

$config['sess_save_path'] = '/opt/lamp/htdocs/codeigniter/sessions/';
$config['upload_url'] = '/opt/lamp/htdocs/codeigniter/uploads/';
$config['no_image_url'] = 'http://localhost/codeigniter/assets/images/no_image.gif';

*the $config['upload_url'] must be set right or no images will be display.

// application/config/routes.php

$route['image/view/(:any)/(:any)'] = 'files/image/view/$1/$2';

// calling the image

<a href="<?php echo base_url(); ?>image/view/member_1/image_name.jpg"  alt="">

// application/controllers/files/Image.php

* Images controller.
defined('BASEPATH') OR exit('No direct script access allowed');

class Image extends CI_Controller {

public function __construct()


$this->CI =& get_instance();

* ------------------------------------------------------
*  Load helper and libraries files.
* ------------------------------------------------------

log_message('info', 'Image Class Initialized');

* ------------------------------------------------------
*  Get file.
* ------------------------------------------------------
public function view()

$folder = $this->uri->segment(3);
$folder = str_replace('-', '_', $folder);
$folder = str_replace(' ', '', $folder);
$folder = trim(preg_replace( "/[^A-Za-z0-9\_\.]/i", "", $folder));

  $file = $this->uri->segment(4);
$file = str_replace('-', '_', $file);
$file = str_replace(' ', '', $file);
$file = trim(preg_replace( "/[^A-Za-z0-9\_\.]/i", "", $file));

$path = $this->config->item('upload_url').$folder.'/'.$file;
$no_image = $this->config->item('no_image_url');

if (file_exists($path))

$mimetype = get_mime_by_extension($path); // will need file_helper loaded

header("Content-Type: ".$mimetype);
header('Content-length: '.filesize($path));

} else {

$mimetype = get_mime_by_extension($no_image); // will need file_helper loaded

header("Content-Type: ".$mimetype);
header('Content-length: '.filesize($no_image));




I forgot to mention that this behavior does't happens with the previous CI3 Developer Version while using the Session_native driver but it does on CI3-RC2 using the Session_files_driver.

Thank You!

Messages In This Thread
Display Above Root Image with a controller on CI3-RC2 - by Mel9pr - 02-07-2015, 07:22 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.