Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include whole content of a file and echo it

Tags:

php

echo

I need to echo entire content of included file. I have tried the below:

echo "<?php include ('http://www.example.com/script.php'); ?>";

echo "include (\"http://www.example.com/script.php\");";

But neither works? Does PHP support this?

like image 848
Elliott Avatar asked May 28 '09 15:05

Elliott


5 Answers

Just do:

include("http://www.mysite.com/script.php");

Or:

echo file_get_contents("http://www.mysite.com/script.php");

Notes:

  • This may slow down your page due to network latency or if the other server is slow.
  • This requires allow_url_fopen to be on for your PHP installation. Some hosts turn it off.
  • This will not give you the PHP code, it'll give you the HTML/text output.
like image 65
ceejayoz Avatar answered Oct 16 '22 19:10

ceejayoz


Shortest way is:

readfile('http://www.mysite.com/script.php');

That will directly output the file.

like image 28
Matt Avatar answered Oct 16 '22 19:10

Matt


Echo prints something to the output buffer - it's not parsed by PHP. If you want to include something, just do it

include ('http://www.mysite.com/script.php');

You don't need to print out PHP source code, when you're writing PHP source code.

like image 37
Adam Wright Avatar answered Oct 16 '22 20:10

Adam Wright


Not really sure what you're asking, but you can't really include something via http and expect to see code, since the server will parse the file.

If "script.php" is a local file, you could try something like:

$file = file_get_contents('script.php');
echo $file;
like image 21
Pavel Lishin Avatar answered Oct 16 '22 20:10

Pavel Lishin


This may not be the exact answer to your question, but why don't you just close the echo statement, insert your include statement, and then add a new echo statement?

<?php
  echo 'The brown cow';
  include './script.php';
  echo 'jumped over the fence.';
?>
like image 37
Lou Morda Avatar answered Oct 16 '22 20:10

Lou Morda