January 23, 2012

CKEditor + KCFinder integration with CakePHP - tutorial

Lately I have been developing an application based on the new version of CakePHP (I am currently switching from 1.3 to 2.0.5).
Together with the change of CakePHP version, I also wanted to implement the new version of WYSIWYG editor - so far I've been using FCKeditor, which is no longer developed and which was replaced with CKEditor. I decided to move on with CKEditor.
CKEditor is also free and open source like FCKeditor, but there is a small difference between these two editors. FCKeditor had a built-in option of file managment (for example browsing server or file upload in insert image file option), which CKEditor doesn't have in the free and open source version. The file manager which can be used along with CKEditor is CKFinder - but this software isn't free. You have to buy licence to use it.
But there is a workaround - we can use a free KCFinder file manager, which can be easily integrated into CKEditor and other editors (FCKeditor, TinyMCE).

For the purpose of tutorial let's assume that our Cake application is being stored on our localhost and the "webroot" of our application is under "http://localhost/".

The following instructions were made based on a following versions of software:
  • CakePHP 2.0.5
  • CKEditor 3.6.2
  • KCFinder 2.51

How to install and integrate CKEditor to CakePHP? 
  1. Download CKEditor from www.ckeditor.com
  2. Copy files from the zipped folder to "webroot/js/ckeditor/"
  3. In the view where you want to display the editor, put the following script on the top of the page (or somewhere before textarea which you want to contain editor):
    <?php echo $this->Html->script('ckeditor/ckeditor');?>
    This scipt will include the "webroot/js/ckeditor.js" file to your view.
  4. Create the textarea and give it a class named "ckeditor"
    <?php echo $this->Form->textarea('content',array('class'=>'ckeditor'))?>
Voila! The editor is now displaying instead of raw textarea.


How to install and integrate KCFinder to CakePHP?
  1. Download KCFinder from kcfinder.sunhater.com
  2. Copy files from the zipped folder to "webroot/js/kcfinder/"
  3. Open "webroot/js/kcfinder/config.php" file.
    In this file you can set several configurations, but we will focus on the ones that are most important for our file manager to work.
    KCFinder is disabled by default.
    If you want to give the ability of using file manager to all users in your website (so with no restrictions), change the value of 'disabled' to false.
    'disabled' => false,
    But giving everyone access to managing files on the server is probably not a good idea. Instead of this, you can enable file managment only when a specified session variable is set. The name of the session variable is on the bottom of config.php file:
    '_sessionVar' => &$_SESSION['KCEDITOR'],
    So, this is what you should do to make the file manager available after an authenticated user logs in:
    • leave the "disabled" to "true"
    • in the controller where you do the user authentication, after user has successfully logged in, enter this code to override the "disabled" value:
      $_SESSION['KCEDITOR']['disabled']=false;
      Remember to destroy this session variable after user logs out.
    • on the top of the "webroot/js/kcfinder/core/autoload.php" file insert:
      session_name('CAKEPHP');
  4. Still in the "config.php" file define the folder for uploaded files. By default the folder is "upload" in "kcfinder" directory.
    If you are developing an application on your localhost and the "webroot" is under a "http://localhost/" address, and you want to keep uploaded files in "webroot/upload" folder, the 'uploadURL' configuration should look like:
    'uploadURL' => "/app/webroot/upload",
    If you are developing your application under for example "http://localhost/my_app/" address, the uploadURL should look like:
    'uploadURL' => "/my_app/app/webroot/upload",
You can test if KCFinder displays and works correctly under the URL:
http://path_to_your_app/js/kcfinder/browse.php


How to integrate KCFinder to CKEditor in CakePHP?
Open the "webroot/js/ckeditor/config.js" file and modify editor config to contain following code:
CKEDITOR.editorConfig = function( config )
{
   config.filebrowserBrowseUrl = '/js/kcfinder/browse.php?type=files';
   config.filebrowserImageBrowseUrl = '/js/kcfinder/browse.php?type=images';
   config.filebrowserFlashBrowseUrl = '/js/kcfinder/browse.php?type=flash';
   config.filebrowserUploadUrl = '/js/kcfinder/upload.php?type=files';
   config.filebrowserImageUploadUrl = '/js/kcfinder/upload.php?type=images';
   config.filebrowserFlashUploadUrl = '/js/kcfinder/upload.php?type=flash';
};

If your app is being developed under for example http://localhost/my_app/, remember to put the whole path in the URLs, for example:
config.filebrowserBrowseUrl = '/my_app/js/kcfinder/browse.php?type=files';
After adding above six lines of code in the CKEditor configuration you should be able to see "Browse" button in image / link / Flash insert options.

And that's it!:-)

40 comments:

  1. Hi, thanks for your post. I do the steps and all works fine except for the 'uploadURL' => "/app/webroot/upload",
    When KCFinder open the window to load images, all is ok. The image was uploaded and showed in the window ok. The problem is when I press the green button (aceptar), and the image paste in the text of ckeditor (it appear like doesn't exist in the server.. Can I modify this path??
    I configured my site like: foo.localhost/
    and I write 'uploadURL' => "http://foo.localhost/app/webroot/upload",
    thanks!

    ReplyDelete
    Replies
    1. Well, it should work that way too.
      What is the URL address of the inserted into ckeditor area image?

      Delete
    2. How can you solve this problem?

      Delete
  2. Hi,
    * the URL in kcFinder is:
    http://foo.localhost/app/webroot/upload/images/test.jpg
    * I check that ~/public_html/svn/foo/app/webroot/upload/images/test.jpg is
    present
    * The url in ckeditor is:
    http://foo.localhost/js/ckeditor/skins/kama/images/noimage.png?t=B8DJ5M3

    When I put http://foo.localhost/app/webroot/upload/images/test.jpg in the
    browser, I get a 500 Internal Server Error. with the following error:

    [Wed Feb 29 16:21:18 2012] [alert] [client 127.0.0.1]
    /home/ppp/public_html/svn/foo/app/webroot/upload/.htaccess: php_value not
    allowed here

    it is necessary to add some line in .htaccess or routes.php of cake?
    I use cakephp 2.05

    ReplyDelete
  3. Manish Kumar ShuklaMay 11, 2012 at 2:04 PM

    Thank you sir...The description was too useful. Thank you very much

    ReplyDelete
    Replies
    1. I'm happy to hear so:)
      Also, I prefer calling me "lady" than "sir" ;-)

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hello,

    Thanks for the post.it's really helpful.
    Though i am getting an error when tried to upload an image.
    http://localhost/mysite/js/kcfinder/browse.php?type=images&CKEditor=content&CKEditorFuncNum=2&langCode=en

    when click Browse button it opens above page but gives me an error like JsController not found.

    have I made any mistake in configuration ?
    my CkEditor Configurations are as below :

    config.filebrowserBrowseUrl = '/mysite/js/kcfinder/browse.php?type=files';
    config.filebrowserImageBrowseUrl = '/mysite/js/kcfinder/browse.php?type=images';
    config.filebrowserFlashBrowseUrl = '/mysite/js/kcfinder/browse.php?type=flash';
    config.filebrowserUploadUrl = '/mysite/js/kcfinder/upload.php?type=files';
    config.filebrowserImageUploadUrl = '/mysite/js/kcfinder/upload.php?type=images';
    config.filebrowserFlashUploadUrl = '/mysite/js/kcfinder/upload.php?type=flash';

    any help will be appreciated.

    Awaiting your reply.

    Thanks in advance

    ReplyDelete
    Replies
    1. Hi,

      I think it must be /mysite/webroot/js... (webroot forgotten?! in this article too)

      But I have the same error, even when I try to change it this way.

      The path seems to be correct and I also changed the folderpermissions to 777 (under Mac) - stange...

      (Kann ich hier auch deutsch schreiben? :-)

      Greetz

      Delete
    2. Hello.

      You should not use webroot in the path.
      It should work without the "webroot" specified in URL. Unless you do not use mod_rewrite (for example it's disabled on server), then you should put "app/webroot" into path, so it looks like:
      /mysite/app/webroot/js...
      I mean... I guess, because I didn't occure any problems with this before.

      Delete
    3. Make sure that the specified files do exist on a server and also - if the path is correct.
      Do you have mod_rewrite working?

      Delete
  6. Hello,

    Thanks for the post!
    Perfect!

    Bye, Mark

    ReplyDelete
  7. Hi,

    Thank you for the post!!!

    Anuj

    ReplyDelete
  8. Hey!


    My config: http://pastebin.com/ZgPu46g1

    And the error message: http://pastebin.com/B1bAzEz0

    Any help, please ?

    ReplyDelete
    Replies
    1. Try removing the "webroot" from path, so it looks like:
      /ck/js/...

      more here: http://cakephpclues.blogspot.com/2012/01/ckeditor-kcfinder-integration-with.html?showComment=1360000490116#c5513685724299745659

      Delete
  9. I got a message "You dont have permission to find file in the server" (or similar).. But if, i set disabled to FALSE, its run normally... how i can set permissions with disabled = true? =/

    ReplyDelete
    Replies
    1. It is described in point 3 of this tutorial:

      "in the controller where you do the user authentication, after user has successfully logged in, enter this code to override the "disabled" value:

      $_SESSION['KCEDITOR']['disabled']=false;"

      You need to override this session value in order to have permissions to manage files.
      Also, you should be using PHP sessions (so make sure what type of sessions you have set in core.php) - with CakePHP sessions overriding this session value does not work for granting permissions.

      Delete
    2. but dont give the permission =/
      See my Controller and autoload:

      http://bin.cakephp.org/view/168671435

      CakePHP 2.3

      Delete
    3. You should move the
      $_SESSION['KCEDITOR']['disabled']=false;
      line before redirect.

      Actions placed after redirect (in this case - rewriting the session variable) just aren't performed, because user is redirected to another page.

      Delete
    4. Oh, same result =/

      any other idea?

      Delete
    5. I had same trouble that I addressed by putting the session code in webroot/js/kcfinder/core/bootstrap.php at the end of file just before closing tag '?>'.

      // PUT YOUR ADDITIONAL CODE HERE
      session_name('CAKEPHP');
      session_start();

      Delete
  10. Be sure that is php who stores session data. In your core.php:

    Configure::write('Session.save', 'php');

    If you let 'cake' to store session data external php files will not retrieve it. Also, remember to set the right name for session

    http://stackoverflow.com/questions/1449593/cakephp-ckfinder-sessions-dont-seem-to-work

    ReplyDelete
  11. Thanks for this tutorial.......

    ReplyDelete
  12. Awesome Tutorial, very helpful.. thanks Chrupka

    ReplyDelete
  13. I create with admin routing but cannot find browse.php ?

    ReplyDelete
  14. Thanks for sharing this wonderful and useful tutorial...

    ReplyDelete
  15. Hi there, you tutorial is very helpful i just have a problem using it with cakephp.
    I Managed to set it up i can upload,edit, etc.. images the only problem is that i can not insert images in to content. I try double click but it doesn't work.
    Currently cakephp installation is in a subfolder so my config is

    'uploadURL' => "/Cake/app/webroot/files",
    'uploadDir' => $_SERVER['DOCUMENT_ROOT']."/Cake/app/webroot/files/",
    and the function

    function openKCFinder(field_name, url, type, win) {
    tinyMCE.activeEditor.windowManager.open({
    file: '/Cake/js/kcfinder/browse.php?opener=tinymce&type=' + type,
    title: 'KCFinder',
    width: 700,
    height: 500,
    resizable: "yes",
    inline: true,
    close_previous: "no",
    popup_css: false
    },{
    window: win,
    input: field_name
    });
    return false;
    }

    Thanks in advace.

    ReplyDelete
  16. Hello, I don't know if this post is still alive, but after a couple of hours browsing, it was the only one that got things moving, so I'll give it a try...

    I followed your instructions, and i got the 'http://localhost/cakephptest/js/kcfinder/browse.php' working (my project is at http://localhost/cakephptest/). I set the 'disabled' value to false, so I think I don't need the session part (although in desperation, I did that too). My config.js file in ckeditor folder looks like this:

    CKEDITOR.editorConfig = function( config )
    {
    config.filebrowserBrowseUrl = '/cakephptest/js/kcfinder/browse.php?type=files';
    config.filebrowserImageBrowseUrl = '/cakephptest/js/kcfinder/browse.php?type=images';
    config.filebrowserFlashBrowseUrl = '/cakephptest/js/kcfinder/browse.php?type=flash';
    config.filebrowserUploadUrl = '/cakephptest/js/kcfinder/upload.php?type=files';
    config.filebrowserImageUploadUrl = '/cakephptest/js/kcfinder/upload.php?type=images';
    config.filebrowserFlashUploadUrl = '/cakephptest/js/kcfinder/upload.php?type=flash';
    };

    And the "Browse server" button does not appear. Please note that I am using CKEditor 4.4.2, so can this be the issue?

    Thank you in advance.

    ReplyDelete
    Replies
    1. Ok, after a couple of hours i located the problem. CKEditor doesn't work well in chrome. I opened it in Mozilla Firefox and everything was good. Will look for my solution elsewhere :)

      Delete
  17. config.filebrowserBrowseUrl = '/blogo/app/webroot/js/kcfinder/browse.php?type=files';
    config.filebrowserImageBrowseUrl = '/blogo/app/webroot/js/kcfinder/browse.php?type=images';
    config.filebrowserFlashBrowseUrl = '/blogo/app/webroot/js/kcfinder/browse.php?type=flash';
    config.filebrowserUploadUrl = '/blogo/app/webroot/js/kcfinder/upload.php?type=files';
    config.filebrowserImageUploadUrl = '/blogo/app/webroot/js/kcfinder/upload.php?type=images';
    config.filebrowserFlashUploadUrl = '/blogo/app/webroot/js/kcfinder/upload.php?type=flash';

    ReplyDelete
  18. Cake PHP Development utilizes an adaptable system for site development which helps in the building of web applications quick. Help for this structure is free and simple to get a hold of.

    ReplyDelete
  19. I try but not working.
    e.g.: http://mysite.com/app/webroot/upload/images/test.jpg =>
    Missing Method in AppController
    Error: The action webroot is not defined in controller AppController
    Error: Create AppController::webroot() in file: app/Controller/AppController.php.

    OR

    e.g.: http://mysite.com/upload/images/test.jpg =>
    Missing Controller
    Error: UploadController could not be found.
    Error: Create the class UploadController below in file: app/Controller/UploadController.php

    rewrite_mod is on in my server. How can i solve this problem?

    ReplyDelete
  20. I have followed your tutorial. An finally the editor is showing :) My only problem is that when I view the page... I am seeing html tags instead of formatted text :/

    ReplyDelete
  21. Hi .. i face a problem ... when i click button browse (image) , the popup come out but it only content text :
    * @copyright 2010-2014 KCFinder Project * @license http://opensource.org/licenses/GPL-3.0 GPLv3 * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3 * @link http://kcfinder.sunhater.com */ require "core/bootstrap.php"; $browser = "kcfinder\\browser"; // To execute core/bootstrap.php on older $browser = new $browser(); // PHP versions (even PHP 4) $browser->action(); ?>

    Can you help me? i dont change anything..

    ReplyDelete
    Replies
    1. I had the same problem because I was adding KCfinder in plugin webroot instead of core webroot.

      Delete
  22. In file config.php
    i modified
    'disabled' => true,
    '_sessionVar' => &$_SESSION['KCEDITOR'],

    I create session $_SESSION['KCEDITOR']['disabled'] = false; in my controller login
    But i dont have any permission when use browser server .
    I also tried include session_start() but it still not work.
    $_SESSION['KCEDITOR'] is NULL
    -----------------------------------------------------
    Could you help me please ?

    ReplyDelete
  23. Thanks for the sharing. Now, I can use the ckeditor in my view.

    ReplyDelete
  24. works like a charme, even in cakephp 3.xx

    ReplyDelete

Thank you for leaving your comment:-)