Integrating CKFinder with the Codeignitor Framework (PHP)

Note (3/9/2013) – This blog post was written a long time ago.  So I am not entirely sure if this fix would still apply.  Although, it most certainly might, if not then something very similar would still work.

I have become very familiar with Codeignitor, CKFinder, and CKEditor since I have been in business. Recently I have had the challenge of trying to integrate the CKEditor and CKFinder systems into the Codeignitor framework.

CKEditor is pretty easy to setup inside Codeignitor. All you have to do is just load all of the ckeditor files/folders into the root directory and then include the JS file and configure it like you would on any normal website. CKFinder is practically the same. All you really have to do is load the files to the server and specify were to find them when you create the instance, as well as do all of the configuration options and initial setup.

I have known how to do this for quite awhile. However recently I was approached with a challenge that I did not readily know how to accomplish. Basically I had multiple different user accounts, and each user account needed to have the ability to have their own folder to use with CKFinder. Normally CKFinder defaults to ckfinder/userfiles and it works all internally. I had a situation where I had to basically have a new folder (named the ID of the user) for each user. I went back and forth into the code trying to work this out, I dug through Google for hours, and I also spent a great deal of time in forums trying to get the answer.

Finally I ran across a stray post in the Codeignitor forums that did exactly what I needed. It was pretty easy to setup, and since I got it initially configured it worked great. Here are the steps I followed to get this implementation up and running with the least amount of stress. It was very affective and it has worked fine ever since it was setup, and it’s been in a production environment for awhile.

1) Setup a new folder in the main directory (the same directory that contains your Codeignitor index file). I named my folder “ckfinder_userfiles”. Once you setup the new folder, load it up to the server.

2) Now open the CKFinder config file. If you loaded your CKFinder folder into the server in the main directory then you can find the config file at the following location: /ckfinder/config.php

3) Once you have the file open we are going to assume that you have a session saved using codeignitor that is called “user_id” for example. It can really be anything but it will serve our purposes for this example. By default you need to look at line 61 but it could change if you have a custom setup for your config file. Your looking for the area where the base_url is setup. Just replace the base url parameter with the following code:

$session = unserialize($_COOKIE[‘ci_session’]);

$user_id = $session[‘user_id’];
$baseUrl = ‘/ckfinder_userfiles/’ . $user_id . ‘/’;

That is it. It will take the internal Codeignitor session and dig out the user_id and use that to build the base url. Now anytime that user is logged in (assuming the user_id is set as a session to their user id, then it will automatically default internally to the folder ckfinder_userfiles/theirid. It also handles all of this automatically so you don’t have to worry about doing anything to the folder or creating it. CKFinder will create it automatically. The good part is this restricts user across the board to only entering their one folder. So this prevents people from getting into other people’s folder (Which can be a problem if you are needing to restrict users to only their specific folders.

There are probably other solutions to this problem. I found this specific solution to be very effective, and seemingly very secure. I would be interested to hear about other people’s thoughts regarding this procedure, or maybe things they have tried in their own implementations. Feel free to comment.

Comment with your Facebook Account

Comment Without Facebook

Leave a Reply

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