PHP - file_get_contents() automatically echoes to the HTML page - php

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!

Related

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/html file is not displayed by server

I am extremely knew to working with databases and servers and just downloaded MAMP. I have a website I made in html and css and want to inject a php script to it. I have saved the file in .php and when I navigate to it from the localhost port on my browser, none of the html is displayed. It only shows a blank white page. There is probably a really obvious answer but I've been searching google for an hour and haven't found a solution.
This is the php script. It is wrapped in a tag everything else in the document is html.
<?PHP
$filelocation = "Text.txt";
if (!file_exists($filelocation)) {
echo "Couldn't find datafile, please contact the administrator.";
}
else {
$newfile = fopen($filelocation,"r");
$content = fread($newfile, filesize($filelocation));
fclose($newfile);
}
$content = stripslashes($content):
$content = htmlentities($content):
$content = nl2br($content);
echo $content;
?>
Most likely there's an error in your PHP code and it can't be parsed. Check the server logs to see what the error message is.
That seems to be valid PHP at a glance.
You could read the file more easily by doing...
$content = file_get_contents($filelocation);
but that's incidental.
Turn on Error reporting in your php.ini file and then restart your webserver. This should give more detailed error information. You should also check your server error logs as there's usually something in there too.
Are you getting an HTTP 500 response code with the blank page? Also, are you sure the file in question actually has any contents?
You have two syntax errors in your code:
$content = stripslashes($content):
$content = htmlentities($content):
They must end with semicolon
$content = stripslashes($content);
$content = htmlentities($content);
Besides that, your errors are likely catched and written to logs as said by others.
Most probably you have a php / php syntax error.
On the first line of your .php file write the following:
<?php error_reporting(E_ALL); ?>
this should make the interpreter to show you the errors you have.
Also, more details about your problem won't hurt.

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

Include whole content of a file and echo it

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

Categories