How to read raw variables from a file in PHP? - php

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.

Related

PHP writing dynamic files with $_SESSION declaration fails

I am trying to generate a dynamic file with php. Within this code, I need to declare some $_SESSION variables. For instance, the redirect url on refresh etc.
However, when I try to declare the $_SESSION variable and use fwrite, it fails to generate the php file.
How can I make sure that I can?
My code:
<?php
header('Content-Type: text/plain; charset=utf-8');
$file = fopen("testfile.php","w");
echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");
fclose($file);
?>
Thanks for your help
as apokryfos stated,
escaping the $_SESSION worked!
echo fputs($file,"$_SESSION['test'] = 'Hello World. Testing!'");
had to be modified to:
echo fputs($file,"\$_SESSION['test'] = 'Hello World. Testing!'");
Thanks!

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!

PHP 'include' file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working on a project which calls many php files via <?php include 'filename'; ?>
What I want to do is I want a php function or code that replaces all include occurences with the actual file, so that all my 6-7 files are converted into one single PHP file. I have to distribute it to many people, so having a single PHP file would be good in that context. Calling that php file would create a new php file with all the included files.
I want to ship my full project in a single file, just as adminer does !
Any idea how to do it ?
For example :-
...php-code...
<?php
include 'dologin.php';
?>
...php code...
would be converted to :-
...php code...
function dologin();{
...(dologin.php file)...
...php code...
You don't want to use include() here, what you essentially want to do is take a bunch of files and combine them into one. There's no need to actually parse the files as you go, you just need to open them, read until the end, and stick what you got into another file - repeating until done.
Something like this:
<?php
$sources = array('file1.php', 'file2.php', 'file3.php');
$out = fopen('final.php', 'w+');
if ($out === false)
die('Could not open output file');
foreach($sources as $source) {
$buff = file_get_contents($source);
fwrite($out, $buff);
}
fclose($out);
?>
Notes:
I do very little error checking here. What happens if you can't open one of the source files, or the length of what you read is wildly different from what you expect?
I do very little error checking here. What happens if fwrite() fails?
I do very little error checking here. Is it safe to just keep appending as I do? Should newlines be injected after each file is written to the output file? Are you sure you won't end up with a missing ?>
I do very little error checking here. No editor is going to accidentally save an input file with a byte-order-mark at the beginning?
You'll of course need to handle doing something with the generated file, and unlinking it once done (though the flags sent to fopen() will truncate it, which is why I went with that family of functions, aside from the convenience of file_get_contents()). Check the manual for more info on how they work.
Honestly, though - depending on your platform, a simple shell script would probably suffice. I'm pretty sure this is what you're trying to do from the extra info you edited into your question.
This is what I exactly wanted. Please improve this php code if you can.
<?php
$a = file_get_contents("sample file");
$match = "/include '.*';/";
preg_match_all($match, $a, $matches);
foreach($matches['0'] as $b)
{
$c = explode("'", $b);
$c = $c['1'];
$temp = file_get_contents($c);
if(preg_match("/<?php/", $temp))
{
$a = str_replace($b, "?>". file_get_contents($c) . " \n ?>\n<?php \n", $a);
}
else
{
$a = str_replace($b, "?>". file_get_contents($c) . "\n<?php \n", $a);
}
}
file_put_contents("combined.php", $a); ?>
I understood that You want to integrate the included file into a function.
I never thought of this question before because it's useless, but maybe you have some "unknown" reasson behind that, so i have to answer.
Here is how:
dologin.php
<?php
... php code
?>
the function:
function dologin(){
global $variable1;
global $variable2;
// paste php code from the dologin.php file here
}
NOTE:
change $variable1 and $variable2 to the variable that may be included from another file in your main file that you're trying to use the function in, if these variables are getting used in the function ithout doing this you'll face the variables scope problem.
That's it now you have a function instead of a PHP file.
Try this...
Note I haven't had a chance to test this but get_included_files gives you everything thats included then you can just build a new file with the contents of all of them.
<?php
...All the include statements ....
$my_includes = get_included_files ();
$file = "everything.php"
file_put_contents($file, "<?PHP");
foreach($my_includes as $included){
$file_contents = file_get_contents($included);
file_put_contents($file, $file_contents, FILE_APPEND);
}
$this_file = __FILE__;
// put the current file in and close
file_put_contents($file, $this_file, FILE_APPEND);
file_put_contents($file, "?>", FILE_APPEND);
?>

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