CodeIgniter 3.0 KCFinder Integration Script

CodeIgniter sessions and PHP native session both works fine.
But not together.

Mixing CI sessions and native session in CodeIgniter always causing troubles.

Script outside CI cannot access CI Session, no even $_SESSION native session created inside CI script.

The real problem is KCFinder use $_SESSION to control various things (Include access control).
But it can’t access anything created by CI scripts.

So I can either make KCFinder read CI session or let CI use native session.

After studying KCFinder source code, I found that it’s accepting CMS integration modules in kcfinder/integration directory.

Unfortunately, only Drupal and BolmerCMS support are provided.

The Solution:

Let’s create by our own integration script.

File: kcfinder/integration/CodeIgniter.php

 *      @desc CMS integration code: CodeIgniter
 *   @package KCFinder-CodeIgniter
 *   @version 0.3
 *    @author Tiger Fok <[email protected]>
 * @copyright 2007-2015 Tiger-Workshop
 *   @license GPLv3
 *   @license LGPLv3
 *      @link
class CodeIgniter {
    static function getSession() {
        define('ENVIRONMENT', 'production');
        define('BASEPATH', realpath(dirname(__FILE__)).'/../../../system/');
        define('APPPATH', realpath(dirname(__FILE__)).'/../../../application/');
        $database = DB($db['default']);
        if (!isset($_COOKIE['ci_session'])) return;
        $database->where('id', $_COOKIE['ci_session']);
        $query = $database->get('ci_sessions');
        $result = $query->row();

        if ($result) {
            $ci_session = self::decode_session($result->data);
            if (!isset($_SESSION['KCFINDER'])) $_SESSION['KCFINDER'] = array();
            if (isset($ci_session['KCFINDER'])) {                
                $_SESSION['KCFINDER'] = array_merge($ci_session['KCFINDER'], $_SESSION['KCFINDER']);
    static function decode_session($session_data) {
        $return_data = array();
        $offset = 0;
        while ($offset < strlen($session_data)) {
            if (!strstr(substr($session_data, $offset), "|")) {
                throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
            $pos = strpos($session_data, "|", $offset);
            $num = $pos - $offset;
            $varname = substr($session_data, $offset, $num);
            $offset += $num + 1;
            $data = unserialize(substr($session_data, $offset));
            $return_data[$varname] = $data;
            $offset += strlen(serialize($data));
        return $return_data;

Usage:  /kcfinder/?cms=CodeIgniter

It copies the CI session to public native session ($_SESSION), so outside script can access $_SESSION.

In your controller, you can now simply use CI session:

$this->session->set_userdata('KCFINDER', array('disabled' => false));

Yes, it’s quite hacky.
But somewhere better to hacking CodeIgniter to make it use native session.

Remember to change the path to match your configuration.

Known problem:

– Sessions is copied in one-way (CI Session => Public native session)
So it retains after CI Session is destroyed.

– You may lose the security improvement by using CI Session over Native session.
If you have such concern, you can modify the script to copy only part of session
Problem resolved by only copying KCFINDER session variable

18 Replies to “CodeIgniter 3.0 KCFinder Integration Script”

  1. Hello,
    you could put to download the full version already with the integrated CI? I am not able to integrate with the KCFinder.

    Can you help me?


    1. Hi,

      I can not properly configure this script.
      I did the following steps:
      1. I created the Codeigniter.php file in the “integration” folder KCFinder.

      2. I put the code ($ this-> session-> set_userdata (‘KCFINDER’, array (‘disabled’ => false));) at the Controller. OK

      3. configured the Codeigniter.php File KCFinder the following points:
      – Define (‘ENVIRONMENT’, ‘production’);
      – Define (‘APPPATH’ realpath (dirname (__ FILE __)) ‘/ .. / .. / .. / application /’.);
      – Require (‘../../../ application / config / database.php’);
      – Require (‘../../../ system / core / common.php’);
      – Require (‘../../../ system / database / DB.php’);
      – $ Database = DB ($ db [‘Develop’]);

      4. I set the config.js File CKEditor with the following:
      – Config.filebrowserBrowseUrl = ‘/admin/assets/ckeditor/kcfinder/browse.php?cms=CodeIgniter&type=files’;
      – Config.filebrowserImageBrowseUrl = ‘/admin/assets/ckeditor/kcfinder/browse.php?cms=CodeIgniter&type=images’;
      – Config.filebrowserFlashBrowseUrl = ‘/admin/assets/ckeditor/kcfinder/browse.php?cms=CodeIgniter&type=flash’;
      – Config.filebrowserUploadUrl = ‘/admin/assets/ckeditor/kcfinder/upload.php?cms=CodeIgniter&type=files’;
      – Config.filebrowserImageUploadUrl = ‘/admin/assets/ckeditor/kcfinder/upload.php?cms=CodeIgniter&type=images’;
      – Config.filebrowserFlashUploadUrl = ‘/admin/assets/ckeditor/kcfinder/upload.php?cms=CodeIgniter&type=flash’;

      Would these steps to setup? But do not know if step 3 is correct, my order of folders is:
      – Design / assets / ckeditor / kcfinder / integration

      The following error message returns me when I open the browser to search for images on the server:

      Warning: require_once(E:\www\project\admin\assets\ckeditor\kcfinder\integration/../../../system/database/DB_driver.php): failed to open stream: No such file or directory in E: \ www \ project \ admin \ system \ database \ DB.php on line 167

      What can this be?


    2. Hi Marcelo,

      You will need to adjust those lines to fit your directory structure.

      define(‘BASEPATH’, realpath(dirname(__FILE__)).’/../../../system/’);
      define(‘APPPATH’, realpath(dirname(__FILE__)).’/../../../application/’);

      Add one or more ../ to those paths accordingly

  2. Ok, I made the changes, but now this returning me a message when I click the button to get a picture immediately after pressing the button is opened and closed the window and returns me the following:

    “You do not have permission to search on the server.”

    I am using the following:
    CI 3.0 + CKEditor 4 + KCFInder 3.12

    Checked CI sessions with a “var_dump” and the session “KCFINDER” is set as:
    ‘KCFINDER’ =>
    array (size = 1)
    ‘disabled’ => boolean false

    I need to make some more changes?

    1. This seems to be KCFinder config issue.
      Does it work by calling /kcfinder/?cms=CodeIgniter directly?

    2. God!
      Sorry to make you waste time on this. I found the error. As I had another older version of KCFinder I decided to download this I wrote to you earlier.
      What happens is that the pattern can KCFinder is disabled.

      I accessed the config.php file KCFinder and changed the following parameter:
      ‘disabled’ => true to false.

      It worked and opened right! I owe this!

      I just have to thank you for supporting fast and for contributing to this script to the CI. English Sorry, I’m using Google translator. I’m from Brazil =)

      Hug and thanks.

    3. Tiger,

      Sorry, but I need more help from you.
      I set all CKEditor with KCFinder, I used your script and ran everything in localhost.

      But now when I put online hosting the following message appears:

      “You do not have permission to search on the server.”

      Can it be session error? You have any idea?


    4. If you have called the integration module correctly (e.g. /kcfinder/?cms=CodeIgniter
      And received this error, yes, it’s a session problem.

      I have created a minimal example (CI + KCFinder).

      In the example, call the welcome page and then /assets//kcfinder/?cms=CodeIgniter it will be accessible.

      I didn’t try CKEditor, but you may need to modify the URL to add the cms=CodeIgniter parameter to call the integration module.

  3. This is working in my localhost (xampp+windows 7), but not working in my hosting server.

    The error :
    Fatal error: Uncaught exception ‘Exception’ with message ‘invalid data, remaining: Ri4Y9–jyN1CshyqM3NMhBnYmDRdBWaVa1al-m7dcwM_xbG4Alz741SDp9af4MkbXbo3yLxX-8ScvNDsPT5OiQ..’ in /home/sagf6924/public_html/assets/kcfinder/integration/CodeIgniter.php:46 Stack trace: #0 /home/sagf6924/public_html/assets/kcfinder/integration/CodeIgniter.php(31): CodeIgniter::decode_session(‘Ri4Y9–jyN1Cshy…’) #1 /home/sagf6924/public_html/assets/kcfinder/integration/CodeIgniter.php(59): CodeIgniter …

    How to fix it?
    Thank You

  4. Why my field blob in table ci_sessions in my localhost is different format with hosting server.
    Blob in hosting server not containing “|” and is encrypted.

    Please help me

  5. Thank you very much. Your script works very well in Edge and firefox, but unfortunally not in chrome. If i cick on the show filemanager button inside the image selector of ckeditor, chrome freezes. Did you encouter this also and if so can you point me in the direction of a solution

    thank you


  6. Dude, just a lil bit problem here ….

    Warning: require_once(D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\integration/../../../system/database/DB_driver.php): failed to open stream: No such file or directory in D:\_LocalWebServer_\htdocs\\system\database\DB.php on line 167

    1. actually this error …
      Fatal error: Uncaught Error: Call to undefined function DB() in D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\integration\CodeIgniter.php:22 Stack trace: #0 D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\integration\CodeIgniter.php(61): CodeIgniter::getSession() #1 D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\core\bootstrap.php(39): require(‘D:\\_LocalWebSer…’) #2 D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\browse.php(15): require(‘D:\\_LocalWebSer…’) #3 D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\index.php(3): require(‘D:\\_LocalWebSer…’) #4 {main} thrown in D:\_LocalWebServer_\htdocs\\assets\plugins\kcfinder\integration\CodeIgniter.php on line 22

  7. Hello,
    I made kcfinder with your script but i can’t find where save any pictures. I checked kcfinder/upload/files directory. but the directory is empty.
    I waiting your help. thank you your scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.