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!
Related
This may be simple to do, sorry if this is quite broad. I am fine with looking for any alternatives, but I would just like some help.
I need to be able to read variables from a file in PHP.
For instance, I would have a file like this:
$test = 10
$greeting = "Hello"
And PHP would be able to take these values and actually use them. The "code" above would be the entire file, add or take a few variables. The reason why I need to do it like this is so that other PHP files can also write variables to it. Is there any method that can do this, or something similar to this.
Thanks
Edit: This file would be a TXT file; it's not meant to be .php
Read lines from your text file:
<?php
$myfile = fopen("variables.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
//echo fgets($myfile) . "<br>";
// <Your code logic here.>
}
fclose($myfile);
?>
For each iteration of the while loop do whatever it is you need to do with the values read in from the text file.
I found a method. I'm still fairly new to PHP, so I'm using it quite strictly. I've decided to make a never ending PHP file, with all my variables in it:
<?php
//File name is variables.php
$test = 10;
$greeting = "Hello";
So then other PHP files can still write to it and other PHP files can read from it:
<?php
include "variables.php";
echo $greeting;
Output would be Hello.
Thank you for all your help everyone.
I simply cant wrap my head around why this simple code won't work on my local wamp server where i have other sites running.
<?php
$file = file_get_contents('C:\Users\Computer\input.txt', true);
echo $file;
?>
The file is where it should be.
I get the following error:
Warning: file_get_contents(C:\Users\Computer\input.txt): failed to open stream: No such file or directory in C:\Users\Computer\Dropbox\htdocs\ny\www\php\day2\code.php on line 3
But the file is exactly where it should be.
The file has all possible permissions to true (read, write ect.) in windows except for "special permissions".
What is not working correctly?
Regards,
Patrick
The solution was the extension. It was not correctly parsed. This was checked by using #VolkerK suggesting to check
Does the output of var_export(glob('C:\Users\Computer*')); contain
input.txt? Or is it false/null?
Thanks to #VolkerK for the help solving the problem.
Since I incorrectly said that it might be because of backslash escaping, I guess I need to redeem by saying something useful :)
So try:
$file = 'C:\Users\Computer\input.txt';
if(is_file($file))
{
$string = file_get_contents($file);
}
else
{
die("Uh oh! Seems like $file has got problems!");
}
Also, you are passing the second argument as true for searching the include path. Since your filepath is absolute, I suspect if you need this argument to be passed at all.
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.
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!
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);
?>