17

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?

trejder
  • 17,148
  • 27
  • 124
  • 216
Elliott
  • 3,812
  • 24
  • 69
  • 93

6 Answers6

24

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.
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
4

Shortest way is:

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

That will directly output the file.

Matt
  • 2,757
  • 19
  • 23
3

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.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • hi, I tried this and get a error saying direct file access is not allowed – Elliott May 28 '09 at 15:32
  • 1
    Sounds like allow_url_fopen is turned off. If your host permits it, you can try putting "php_value allow_url_fopen 1" in a .htaccess file. – ceejayoz May 28 '09 at 15:33
  • His question was how to do a particluar task, not whether or not eh should. Telling him what he does and does not need to do does not help answer the question. Unless asked, his strategy is up to him. – KOGI May 28 '09 at 18:23
  • This is better as you want the file to still be parsed – James Baird Sep 29 '22 at 11:11
1

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.';
?>
Lou Morda
  • 5,078
  • 2
  • 44
  • 49
1

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;
Pavel Lishin
  • 129
  • 7
0

Matt is correct with readfile() but it also may be helpful for someone to look into the PHP file handling functions manual entry for fpassthru

<?php

$f = fopen($filepath, 'r');

fpassthru($f);

fclose($f);

?>
techbio
  • 31
  • 4