0

I'm making a small application to let me edit files from my browser. Typically these will be PHP files. I'm trying to figure out a way that I can have PHP on the page whilst printing the contents of the file as text/plain so they are not executed.

I've looked into setting

header("content-type: text/plain");

However if I include this none of the PHP renders on the page, whilst I would like just the file I'm printing to be in plain text. I've also played about with code and pre tags to no avail as the server keeps executing the PHP.

1
  • 1
    That header shouldn't affect how the PHP is executed, only how the browser views what it receives. Commented May 27, 2015 at 13:39

4 Answers 4

4

You should change nothing in the way you output your normal edit page.

You can read the php file using for example file_get_contents() and output it directly in your textarea. Note that you need to make sure that the contents of your php file don't break the html:

<textarea name="contents">
<?php 
echo htmlspecialchars(file_get_contents('your_file_to_be_edited.php'));
?>
</textarea>
Sign up to request clarification or add additional context in comments.

Comments

3

I think you are probably looking for highlight_file(). It even includes syntax highlighting.

Just use it like this:

highlight_file("phpFile.php"); 

3 Comments

Maybe I am missing something, but how would this allow the OP to edit the file? If anything, it would add html markup to the php source code.
@jeroen Well as I understand it: as text/plain so they are not executed OP has a part, where he wants to print the file and then he has some other code, where he can edit the file.
Multiple interpretations possible, as always ;-)
2

You can use htmlspecialchars function (converts special characters to HTML entities):

<?php echo htmlspecialchars($fileContent); ?>

There is also an opposite function - htmlspecialchars_decode (converts special HTML entities back to characters).

Comments

0

For download:

<?php
header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');


echo "this is the file\n";
echo " you could generate content here, instead.";
?>

For display:

highlight_file("yourFile.php"); 

2 Comments

That's how to download a text file, pretty different than how to show a php one for editing
Ah yes. I understand that you whant to download.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.