Hi there, I want to grab a page from my site that's PHP, however, I don't want it to display the PHP...
How can I do this?

Recommended Answers

All 13 Replies

Below is example to get Google's content.

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

Is this what u want?

Below is example to get Google's content.

<?php
$homepage = file_get_contents('http://www.google.com/');
echo $homepage;
?>

Is this what u want?

Hmm.. Not really, it's a file located in my site.. Something like

echo file_get_contents("./data.php");

But it displays the PHP code in data.php too =\

echo file_get_contents("./data.php");

code will not display any php tag. It shows HTML output of file data.php.
You want to display php tag also?

echo file_get_contents("./data.php");

code will not display any php tag. It shows HTML output of file data.php.
You want to display php tag also?

Interesting, I made a page named data.php that contained user data, it outputted it as text. However, when I try to use echo file_get_contents("./data.php"); It also includes the PHP, not just the text it outputs.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

Exact Example:

index.php
<?php echo file_get_contents("./lol.php"); ?>

lol.php
<?php echo "lol"; ?>

index.php Outputs <?php echo "lol"; ?> instead of lol.

how could it display php tags? its a scripting language and when you echo it it automatically executed.
Post your exact output if issue is still not solved.

-vib
That is not true. When you use something like file_get_content() it does not parse/execute what it reads in. If you included the file then yes it would execute any php in that file.

-Gigs
This appears to me like you're trying to use it for some kind of template system? Where your output has a series of echo statements and you've already set some variables that should be replaced.

ob_start();
include ('lol.php');
$content = ob_get_contents();
ob_end_clean();

echo $content;

That code will include lol.php which will execute any php statements within it and then capture the content to a variable which you can then use at a later time. Is this more what you're trying to accomplish?

-vib
That is not true. When you use something like file_get_content() it does not parse/execute what it reads in. If you included the file then yes it would execute any php in that file.

-Gigs
This appears to me like you're trying to use it for some kind of template system? Where your output has a series of echo statements and you've already set some variables that should be replaced.

ob_start();
include ('lol.php');
$content = ob_get_contents();
ob_end_clean();

echo $content;

That code will include lol.php which will execute any php statements within it and then capture the content to a variable which you can then use at a later time. Is this more what you're trying to accomplish?

The function will not internally parse it.
But lets say i have one php page : http://www.mysite.com/register.php
And i use file_get_contents('http://www.mysite.com/register.php') then html source will be returned by file_get_contents function.

Simply we can have 'View Source' type of functionality.

Thanks guys! mschroeder's solution seemed to fix it :)

The function will not internally parse it.
But lets say i have one php page : http://www.mysite.com/register.php
And i use file_get_contents('http://www.mysite.com/register.php') then html source will be returned by file_get_contents function.

Simply we can have 'View Source' type of functionality.

From the PHP.net documentation:
"file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance."

If the file is stored locally, file_get_contents() basically does the following:

$fp = fopen($file, "r");
$contents = fread($fp, filesize($file));
fclose($fp);

It will not execute any PHP contained within the file. For any PHP code within the file to execute you will have to use output buffering and include the file, as mschroeder suggested, or grab the file with cURL (which is similar to grabbing the source from a browser). By far, including the file is faster since it doesn't require any socket connections like cURL does.

From the PHP.net documentation:
"file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance."

If the file is stored locally, file_get_contents() basically does the following:

$fp = fopen($file, "r");
$contents = fread($fp, filesize($file));
fclose($fp);

It will not execute any PHP contained within the file. For any PHP code within the file to execute you will have to use output buffering and include the file, as mschroeder suggested, or grab the file with cURL (which is similar to grabbing the source from a browser). By far, including the file is faster since it doesn't require any socket connections like cURL does.

Thanks for explanation :)

I found an EASIER way to do it! :D include("lol.php") XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.