Posts Tagged ‘HTML editor’

Reading a file with FCKeditor

Friday, May 9th, 2008

I am working on a website which contains sections which the client wants to be able to update themselves.  Not very difficult stuff, just updating their pricelist, special offers, that sort of thing.  Rather than a full blown CMS (Content Management System) I opted to have the content in HTML files which can be included into the webpage as required.

All quite simple really, but I needed a way for the client to be able to edit the content online, a simple HTML editor would do but it needed to be WYSIWYG as, while the client is quite capable of updating a formatted document they don’t really want to get into HTML markup.  After a bit of research I decided that FCKeditor looked pretty good.

Looking at comments about FCKeditor it seemed that it should be pretty easy to set up and indeed it was.  Installation was easy as was getting a quick sample page working, but could I figure out how to load a file into the editor?  No I couldn’t!  I knew it must be fairly simple but I just couldn’t find any example of how to do it.  Putting a string into FCKeditor using the Value parameter was straightforward but getting an actual file in there just confounded me.

After spending most of a day hunting around the internet I just couldn’t get it working properly.  I think I must just be more stupid than the average FCKeditor developer.  But the next day ( problems often look easier the next day) after a little more searching I managed to figure out how to load a file into FCKeditor.  So I thought I’d put the solution on this blog to save anyone else as silly as me a lot of time.

It is of course quite simple.  The value parameter for FCKeditor must be a string, this is then the starting text in the editor window, the trick is to make your file into a string.

Using PHP I prepared the following test page;

include_once(“fckeditor/fckeditor.php”) ;
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
<html xmlns=””>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Amend Pricelist</title>
<form action=”sampleposteddata.php” method=”post” target=”_blank”>

// Read the file into an array
$file_array = file(“pricelist.php”);

// Convert the array into a string
$file_contents = implode(‘ ‘,$file_array);

$oFCKeditor = new FCKeditor(‘FCKeditor1’) ;
$oFCKeditor->BasePath = ‘/fckeditor/’ ;

// Use the string as the initial value for FCKeditor

$oFCKeditor->Value = $file_contents ;

$oFCKeditor->Width  = ‘700’ ;
$oFCKeditor->Height = ‘750’ ;
$oFCKeditor->Create() ;
<br />
<input type=”submit” value=”Submit”>

And it worked!  I had managed to load a file into FCKeditor.  There may be a better way of doing this, and I would love to hear about it if someone knows better, but it does the job.

I hope this blog saves someone else a bit of headscratching.