Wednesday, November 27, 2013

How to display contents from a file into textarea - PHP

How to display contents from a file into the textarea of HTML?

This is very easy and slightly tricky. Here is the PHP code:

    print '<textarea>';
    include($_some_file_name);
    print '</textarea>';


Basically you are including the whole contents from the file into the PHP source which later rendered as HTML. Its just a tricky but effective solution.


1 comment:

Anonymous said...

Please, please don't do this. This will execute any PHP code in the file (which, if the file name can be arbitrarily specified, is a giant security hole). It will also not escape any HTML in the file, so tags will be rendered. If the file is:

</textarea><script>alert("hi");</script><textarea>

... you won't get any text in the textarea and instead end up with an alert box.

The correct way to do this avoiding both execution issues is:

print '<textarea>';
print htmlspecialchars(file_get_contexts($_some_file_name));
print '</textarea>';

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...