1

I have one php page (index.php) with following contents:

<php
    include("{$_SERVER['DOCUMENT_ROOT']}/somefunction.php")
    echo "hello world";
?>

Now I want to read the contents of index.php via another php-file (test.php) The result I want to get is:

line 1: include("{$_SERVER['DOCUMENT_ROOT']}/somefunction.php")
line 2: echo "hello world";

This is what I already tried, but it won't work:

$phppage="{$_SERVER['DOCUMENT_ROOT']}/index.php";
$handle = fopen($phppage, "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        echo $line;
    }
    fclose($handle);
}
else {
    // error opening the file.
    echo "An error occured";
}

It just echoes an empty string.

Problem solved :-)

8
  • Did you try file_get_contents? Commented Feb 27, 2015 at 10:27
  • I eventually want to put the lines in an array. I want my browser to show the php-code instead of hello world Commented Feb 27, 2015 at 10:30
  • is your page source is empty.??? check it please. Commented Feb 27, 2015 at 10:31
  • Place your php code within <pre> </pre> you will get it Commented Feb 27, 2015 at 10:33
  • 1
    i think it is because it contain php tags... Commented Feb 27, 2015 at 10:35

5 Answers 5

2

Actually your code is working. The problem is it will not displayed on browser . Since it contain html special character like <

So either print The result with in a <pre> </pre> tag.

or use htmlentities()

echo htmlentities($line);

or remove <?php ?> from the file index.php

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

1 Comment

Thx, that works! Sorry I can't vote you up... Still new here. Can someone else vote him up plz?
0

When you say "it won't work" - do you get any error messages? Have you tried switching on error_reporting and display errors?

This is possibly security related - no permission to open the file.

Try using a relative path and double check that there are no basedir restrictions in place.

Comments

0

Are you looking for this...

show_source()

This function shows your PHP code in the browser..

URL: http://www.w3schools.com/php/func_misc_show_source.asp

2 Comments

That would be good, if I could put the lines of showsource in an array. I'll test it.
I can't get it in an array, so it's not useful to me. Thx anyway!
0
this is working try this 

read.php as 

<?php
$phppage=realpath("index.php");
$data = htmlentities(file_get_contents($phppage));
echo $data;

?>

index.php as

<?php

$absolute_path = realpath("somefunction.php");
include($absolute_path);
echo "hello world";
?>

1 Comment

I already tried something else, and that works, but this looks like a valiable alternative.
0

If you don't need the line numbers, which are not made by your code, you could do:

readfile('index.php');

or

echo file_get_contents('index.php');

Which will work as long as you work in the same directory. If you need line numbers:

$lines = explode("\n",file_get_contents('index.php'));

foreach ($lines as $key => $line) echo "line {$key+1}: $line\n";

And if you don't want to echo, well, that's obvious.

1 Comment

There is an error in your code (you forgot the close ) on the first line :-)

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.