Wednesday, November 27, 2013

PHP: How to display contents from a file into textarea

Share at Facebook

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>';
    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:


... 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>';