I've got a .php file with javascript, php, and html. I want to include a button/link to view the source of the entire file "pretty printed", but I can't seem to get anything to work. What is the best way to do this?

share|improve this question

69% accept rate
feedback

2 Answers

If you simply want to display the code of the file, you can call this function:

function echoFile($pathToFile){

  $handle   = fopen($pathToFile, "r");
  $contents = fread($handle, filesize($pathToFile));
  fclose($handle);
  $contents = str_replace("<", "&lt;", $contents);
  $contents = str_replace(">", "&gt;", $contents);
  echo "<pre>$contents</pre>";
}

So, if you want to display myPhpFile.php, just do

echoFile("myPhpFile.php");

As far as making it prettified, follow the link that was commented.

share|improve this answer
1  
I'd recommend also replacing & with &amp; to avoid the character being parsed as an entity. Also, do this before replacing < and > to avoid re-escaping the escaped angle brackets. – Delan Azabani Feb 27 '11 at 11:47
feedback

Some combinations of Apache/PHP are configured so that files ending in .phps aren't run as PHP, but instead, highlighted and prettified.

Example:

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.