views:

82

answers:

3

I am using a wysiwyg editor in a web app. It is FCKeditor. In order to edit a file, other than loading javascript, my web form that edits the the file looks like this:

<textarea><?php include('myWebDoc.html') ?></textarea>

I also tried this:

<textarea><?php file_get_contents('myWebDoc.html') ?></textarea>

Both attempts end up parsing the php inside the web document before it gets to the editor.

Is there a better php function or way to get the contents of a file into the textarea tag without parsing?

+1  A: 

This might possibly work:

<textarea><?php readfile('myWebDoc.html'); ?></textarea>

But I think you might have to escape the HTML to make it work properly - I don't know for sure:

<textarea><?php echo htmlspecialchars(file_get_contents('myWebDoc.html')); ?></textarea>
Ray Hidayat
You **do** have to escape HTML.
Alix Axel
+2  A: 

The correct form should be something like this:

<textarea><?php echo htmlspecialchars(file_get_contents('/path/to/file.html')); ?></textarea>

Depending on the contents of the HTML file you might also want to try htmlentities().

Alix Axel
htmlspecialchars() did not work
Dr. DOT
-1 for -1ing me one and for not using readfile instead of file_get_contents for outputting a file straight to the buffer. ;)
no
@no: I'm not getting into a micro optimization discussion here, I still believe `file_get_contents()` is probably faster but that isn't the issue. `readfile()` will output directly to the browser, that means it will break the HTML if the file has any HTML special char. That was the reason for my down vote (and for not using `htmlspecialchars()` at all), got it?
Alix Axel
@Dr. DOT: Could you share the contents of the HTML file? Regarding your comment on @no answer: you should **definitely** use `htmlspecialchars()` (or similar, like `htmlentities()`), otherwise it can break your HTML.
Alix Axel
Alix, readfile will output directly to the current buffer, not necessarily the browser. I never said not to use htmlspecialchars or similar. An output buffer callback could easily be used to process the text before insertion if necessary. And it's not necessarily necessary... It should only screw up if the inserted file contains a closing `</textarea>` tag. He's editing a document; chances are there's no `</textarea>` in the document, and for all I know FCKeditor will not treat escaped HTML as HTML at all (see comment from OP on my answer).
no
@no: I'm sorry but IMHO the OP is wrong. `htmlspecialchars()` should always be used when displaying (XML, HTML, PHP, whatever) tags inside another tag (in this case a textarea). Now, upon submission he may or may not want to use `htmlspecialchars_decode()` - but that's another question. Your answer didn't cover this at all, and certainly didn't provide a **solution**. I've my doubts if `readfile()` is faster than `file_get_contents()` - probably consumes less memory. To be continued...
Alix Axel
@no: But I rather do `echo htmlspecialchars(file_get_contents('file.html'));` than do `ob_start(); readfile('file.html'); echo htmlspecialchars(ob_get_clean());` - if I got your suggestion right! Anyway, this is pointless and the OP probably doesn't care so I'll stop wasting my time.
Alix Axel
+1  A: 

Try readfile instead of file_get_contents.

http://www.php.net/manual/en/function.readfile.php

no
`file_get_contents()` is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
Alix Axel
He doesn't want to read it into a string, though, he wants to output the contents to the buffer. This means the file could be streamed instead of allocating all the memory for the string and then outputting that. In other words, if anything performance should be better, not worse.
no
I have my doubts, still `readfile()` doesn't solve the problem at all.
Alix Axel
readfile solves the problem of the omitted echo. Care to expound on your 'doubts?'
no
Dr. DOT
How come this is the accepted answer?
Alix Axel
Because it's correct.
no
@Dr. DOT: What's wrong with `<` and `>` inside the textarea? That's correctly formed HTML and would be exactly the result produced if you manually typed `<` and `>` inside it.
Alix Axel
Dr. DOT
So, if I let the text editor translate <?php into <?php and save it back to the server in that form -- then the server will no longer parse my php.IMO that's what's wrong
Dr. DOT