Include whole content of a file and echo it - php

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?

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.

Shortest way is:
readfile('http://www.mysite.com/script.php');
That will directly output the file.

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.

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;

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.';
?>

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);
?>

Related

How can I print php variables in text file?

I want to print php variable in a text file.
Example :
<?php
$name='x' ;
?>
I want to put name value in text file for print it as hard copy .
Following is how to accomplish it.
<?php file_put_contents('filename', 'content');
Have a look at this function at PHP.net
Since your question does not include anything you have tried on your own (Which is generally recommended). Here is somewhere for you to start - the php file_put_contents function.
You can try something like this:
<?php
$file = 'some-file.txt';
$name = "x";
// Write the contents to the file
file_put_contents($file, $name);
?>
A few Google searches will surely provide some insight here as well.
Hope it helps!

PHP fgets() not recognizing <?php

I am trying to parse the first few lines of a file however I just noticed, if the first line of the file is "<?php" it won't echo. However, if the first line of the file is "test" it will. Any ideas as to why?
<?php
$file= fopen("testfile.php", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);
?>
I assume you use a default content type text/html, hence <?php is recognised as an HTML tag and your browser doesn't display it. Check the source code of your page.
Here is a nice presentation how to do it: http://www.wikihow.com/View-Source-Code.
This should work for you! (Even with colors!)
highlight_file("testfile.php");
The solution for me is just not to echo the line to the browser. I really don't need it in the browser. In fact, I need other things from the file but just noticed that it was not able to be echo's so I thought I'd ask. Thanks to everyone for your help!

Read php source from another php script

I have two files reader.php and somesource.php. Both in the same folder.
somesource.php content inside the php tag.
echo "hello World";
reader.php contents
$fp = fopen('somesource.php','r') or die($php_errormsg);
$string = fread($fp,filesize('somesource.php'));
echo $string."<br>";
I am expecting to output
echo "hello World";
But I am seeing a blank page. I even tried. curl and file_get_contents. Both with the same output. If I write anything outside the php tag wil be echoed as normal. anything inside the php tag is skipped.
Please Help
use
echo htmlspecialchars($string)."<br>";
Depending on your server setup it may not read it without <?php echo $stuff; ?> in there. <? aka short tags can break a lot of stuff in my experience.

PHP - file_get_contents() automatically echoes to the HTML page

I have a page on a remote server with the following line:
$contents = file_get_contents($search_url);
which automatically echoes $contents to the HTML page no matter what I do. It's as if I have done the following:
$contents = file_get_contents($search_url);
echo $contents;
What could be causing PHP to do this? Is there any configuration item that needs changing?
It might be worth taking a look at this bit of your code. If you comment it out, does the same thing still happen?
<?php if (isset($debug) && isset($ret_value)):?>
<pre>
<?php print_r($ret_value) ?>
</pre>
<?php endif; ?>
This does appear to echo what you have previously fetched in the file_get_contents().
It was a STUPID bug - the variable $contents is being used in the included file as well as in the function it is included from. This caused the issue - nothing wrong with file_get_contents!

Include text in file without <?php ?> tags [file_get_contents]

I have noobish question for you guys. I'm making my private sandboxie where you can test your code. So for now what I'm making is that you write the code, code is executed and you can see the result BUT i want the code you written in the textbox.
Now the problem is, I want that text from .php file to be shown without tags. I might be idiot but I just don't remember how to do it.
so actually code is like
if($_GET['f']){
echo file_get_contents("files/".$_GET['f'].".php");
}else{
echo "echo \"Hello world\";";
}
if you don't understand what I want I can post more info etc but I think its obvious:)
Do you mean something like
<?php
echo str_replace(array('<?php', '?>'), '', file_Get_contents('file.php'));
?
The include function :)
include("files/" . $_GET['f'] . ".php");
But! Never read a file directly from $_GET. Someone could put in ../../sensitiveinformation.txt or something.
Use include(); and require(); would help you include the file. Use eval(); to execute the code.
To see the actual source of the file and strip the <?php ?> tags do this
$file = file_get_contents('time.php');
$ll = array('<?php','?>');
echo str_replace($ll, "", $file);

Categories