{"id":266,"date":"2012-03-22T16:30:26","date_gmt":"2012-03-22T16:30:26","guid":{"rendered":"http:\/\/www.cotsweb.com\/blog\/?p=266"},"modified":"2013-05-17T08:43:13","modified_gmt":"2013-05-17T07:43:13","slug":"integrating-class-upload-php-into-ckeditor","status":"publish","type":"post","link":"https:\/\/www.cotsweb.com\/blog\/integrating-class-upload-php-into-ckeditor-266.html","title":{"rendered":"Integrating class.upload.php into CKeditor"},"content":{"rendered":"<p>I have been working on a Content Management System for <a title=\"Kate's Home Nursing, palliative care charity based in Stow on the Wold\" href=\"http:\/\/www.kateshomenursing.org\" target=\"_blank\">Kate&#8217;s Home Nursing<\/a>, a Stow based charity who provide top quality palliative care around the North Cotswolds.\u00a0 As part of the CMS I am using <a title=\"CKeditor Open Source WYSIWYG editor for websites\" href=\"http:\/\/ckeditor.com\/\" target=\"_blank\">CKeditor<\/a>, which is a very good WYSIWYG editor, allowing the users to create their own content in something like a word processing environment.<\/p>\n<p>But I need the facility to upload images to be integrated into the page being edited, <a title=\"CKfinder Ajax File Manager\" href=\"http:\/\/ckfinder.com\/\" target=\"_blank\">CKfinder<\/a> is a possibility, it integrates well with CKeditor, but it isn&#8217;t Open Source.\u00a0 <a title=\"KCfinder Open Source image upload and file browsing\" href=\"http:\/\/kcfinder.sunhater.com\/\" target=\"_blank\">KCfinder<\/a> is another possibility;\u00a0 it\u00a0 is Open Source but I found it difficult to do exactly what I wanted with it.\u00a0 What I really wanted was to use <a title=\"class.upload.php file upload script\" href=\"http:\/\/www.verot.net\/php_class_upload.htm\" target=\"_blank\">class.upload.php<\/a> which is Open Source and I know from experience does exactly what I want it to do.<\/p>\n<p>The problem was that while I found some useful hints I couldn&#8217;t find clear instructions on the whole process of how to integrate class.upload.php with ckeditor.\u00a0 So after some trial and error here is my solution. <\/p>\n<p>Please note, class.upload.php is a file uploader, it doesn&#8217;t offer the file browser facilties that CKfinder and KCfinder provide.  I have concentrated on the integration process rather than on the various parameters, both CKeditor and class.upload.php are quite well documented and once you have the upload process working it is just a matter of fine tuning until you get the result you want.<\/p>\n<h2>Getting CKeditor to call your custom upload script<\/h2>\n<p>I am calling CKeditor with javaScript from within a webpage so my calling code looks like this;<br \/>\n<pre><code class=\"preserve-code-formatting\">&lt;!-- Set up CKeditor --&gt;\n&lt;script src=&quot;ckeditor.js&quot; type=&quot;text\/javascript&quot;&gt;&lt;\/script&gt;\n\n&lt;script type=&quot;text\/javascript&quot;&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;function displayCkeditor(id)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(CKEDITOR.instances[id])\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CKEDITOR.remove(CKEDITOR.instances[id]);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CKEDITOR.replace(id,&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filebrowserUploadUrl :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;uploader.php?type=files,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filebrowserImageUploadUrl : &quot;uploader.php?type=images&quot;,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filebrowserFlashUploadUrl : &quot;uploader.php?type=flash&quot;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&lt;\/script&gt;\n&lt;!-- CKeditor end --&gt;\n<\/code><\/pre><br \/>\nThis just tells CKeditor to use my custom &#8220;uploader.php&#8221; script when the user clicks on the image upload facility in the editor.  In this case it is expecting to find uploader.php in the current directory (the Ckeditor directory).<\/p>\n<h2>Creating the Custom Upload Script<\/h2>\n<p>And this is the script called by Ckeditor;<br \/>\n<pre><code class=\"preserve-code-formatting\">\n&lt;?php\n\/**\n * uploader.php\n * Use class.upload.php to upload images etc.\n *\/\ninclude(&#039;\/includes\/class.upload.php&#039;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ where you have put class.upload.php\n\n$msg = &#039;&#039;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Will be returned empty if no problems\n$callback = ($_GET[&#039;CKEditorFuncNum&#039;]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Tells CKeditor which function you are executing\n\n$handle = new upload($_FILES[&#039;upload&#039;]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Create a new upload object \nif ($handle-&gt;uploaded) {\n&nbsp;&nbsp;&nbsp;&nbsp;$handle-&gt;image_resize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = true;\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Create a small image (thumbnail)\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\n&nbsp;&nbsp;&nbsp;&nbsp;$handle-&gt;image_x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 150;\n&nbsp;&nbsp;&nbsp;&nbsp;$handle-&gt;image_ratio_y&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= true;\n\n&nbsp;&nbsp;&nbsp;&nbsp;$handle-&gt;process($_SERVER[&#039;DOCUMENT_ROOT&#039;] . &#039;\/userfiles\/&#039;);&nbsp;&nbsp;\/\/ directory for the uploaded image\n&nbsp;&nbsp;&nbsp;&nbsp;$image_url = &#039;\/userfiles\/&#039; . $handle-&gt;file_dst_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ URL for the uploaded image\n \n&nbsp;&nbsp;&nbsp;&nbsp;if ($handle-&gt;processed) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $handle-&gt;clean();\n&nbsp;&nbsp;&nbsp;&nbsp;} else {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg =&nbsp;&nbsp;&#039;error : &#039; . $handle-&gt;error;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n$output = &#039;&lt;script type=&quot;text\/javascript&quot;&gt;window.parent.CKEDITOR.tools.callFunction(&#039;.$callback.&#039;, &quot;&#039;.$image_url .&#039;&quot;,&quot;&#039;.$msg.&#039;&quot;);&lt;\/script&gt;&#039;;\necho $output;\n?&gt;\n<\/code><\/pre><br \/>\nIn this simple example I just upload an image and resize it to a 150px wide thumbnail for inclusion in the edited text.<\/p>\n<p>The important part is the $output echoed back to CKeditor which tells it which function to link up with and the URL of the uploaded file (which will then be inserted into the text being edited).  If the upload is successful the $msg field should be empty, otherwise it should contain an error message and the URL should be empty.<\/p>\n<p>The code above is a working outline, obviously a bit more needs to be built in to make it a safe and useful bit of code but I have left it quite simple so that it is clear and useful for others to build on.  I hope you find it useful.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>I have been working on a Content Management System for Kate&#8217;s Home Nursing, a Stow based charity who provide top quality palliative care around the North Cotswolds.\u00a0 As part of the CMS I am using CKeditor, which is a very good WYSIWYG editor, allowing the users to create their own content in something like a [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,19,18],"tags":[102,46,145,144],"_links":{"self":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/266"}],"collection":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":9,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/posts\/266\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cotsweb.com\/blog\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}