I have the following code:
<?
$serverurl = $_SERVER["DOCUMENT_ROOT"];
$file = $serverurl.'/demo/sample_php.php';
$newfile = $serverurl.'/demo/sample_php.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
$homepage = file_get_contents($serverurl.'/demo/sample_php.txt');
?>
<pre class="code">
<code class="php boc-html-script">
<? echo htmlentities($homepage, ENT_QUOTES); ?>
</code>
</pre>
<? unlink($newfile); ?>
This basically copies a *.php file to a *.txt file, displays the contents, then deletes it. However, I don’t want to create a visible file, as the application is designed to display a list of files, then display the contents of the file. Having a file appear with a .txt extension would be confusing.
I realize I could create a folder that is hidden, and do all my converting there, but I am thinking there must be a more efficient way to display the contents of a php file.
I did some experimenting with tmpfile(), but I couldn’t get the contents of the php file to write to it.
Any ideas?
There is no reason to do the file copy.
file_get_contents()returns the contents of the file as a string, which is all you need. It will not parse and execute the PHP code asinclude()/require()would.Just retrieve the contents of the PHP file into the
$homepagevariable and echo it out as you have done with the temporary text file.After suggestions in the comments to print with highlighting, you can do it more easily with
highlight_file():