0

I have a PHP file. I want to read the PHP code written inside that file. The script I am executing is in the file 'test.php'.

<?php
$test = file_get_contents('test.php');
echo '<pre>'.$test.'</pre>';
?>

The output in browser window is:

'.$test.'

'; ?>

another test on the file:

<!DOCTYPE html>
<html>
<head>
  <title>Canvas Home</title>

  <link rel="shortcut icon" href="http://localhost/collaborate/icons/collaborate.ico">
<?php
    echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheets/".basename(__FILE__,'.php').".css\">";
    echo "<script type=\"text/javascript\" src=\"js/".basename(__FILE__,'.php').".js\"></script>";
?>
  <link rel="stylesheet" type="text/css" href="stylesheets/navs.css">
</head>

<body>
  <div></div>
</body>

</html>

gave the output:

"; echo ""; ?> 

Can someone explain me what is happening in the first file? I expected the returned string to be simply echoed.

Also in the second file, the first simple HTML part is echoed and hence gets interpreted as HTML, understood. But what happens then? Why is the echo command itself displayed?

I basically want to achieve reading the source of any script file rather than it being executed, i want it displayed in the browser window.

4
  • Use your browser's view source feature (Ctrl+U or so) Commented Jul 8, 2013 at 15:06
  • Do you want to read contents of the file, or include it? Commented Jul 8, 2013 at 15:07
  • a side remark : be VERY carefull with what you'll be displaying, and to whom. You're basically opening a window on your source code, that's never a good idea and should be used for very specific cases... Commented Jul 8, 2013 at 15:08
  • @johannes i would not have asked this question if i wanted to view the source, and I am not trying to view some kind of server side script source of any website. I just want to read the html, or any other script on my local system, through php ofcourse in this context Commented Jul 8, 2013 at 15:18

2 Answers 2

4

Since you're outputting the raw PHP code to the browser, the browser is trying to render <?php as an HTML tag. You'll probably want to run the code through htmlspecial chars so any HTML metacharacters in the PHP code are sent encoded, and then NOT rendered by the browser. e.g.

$code = file_get_contents('script.php');
echo '<pre>', htmlspecialchars($code), '</pre>';

And note that your browser LIES to you about output, especially when you're working with PHP and/or HTML. Remember that the browser's job is to RENDER html, and anything that LOOKS like HTML, including PHP tags. If you get wonky looking output in the browser, ALWAYS do a "view source" to see the raw 'code' of the page - you'll probably see your PHP and html code there, coming through just fine.

Sign up to request clarification or add additional context in comments.

7 Comments

echo accepts multiple command-separated arguments. why force php to concatenate multiple strings when the concatted result is simply going to be thrown away right away anyways?
Have you ever benchmarked it? Can't believe this is really of any significance.
for small things like this, it's a minor optimization. but every little bit counts, sometimes. consider the savings if you're outputting two+ very long strings.
Could use [the highlight function][php.net/manual(/en/function.highlight-file.php)
@MarcB is it possible to see the PHP code in the browser (the client side) ? Because as far as my knowledge goes it is only on the server side.
|
0

Your browser is trying to interpret the file as HTML code, as has been pointed out by Mark B. A clean solution would be to make the browser interpret the response body as plain text, like this :

<?php
header('Content-Type: text/plain');
echo file_get_contents(__FILE__);
exit;

Note: In PHP __FILE__ always refers to the current file.

1 Comment

And how do I revert back to the normal interpretation by browser by changing the header? Thanks..!

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.