1

let's say i have 2 files: a.php and b.php

a.php

$value = "test";

i would like to update the $value by using b.php. i want to run b.php and change a.php like that:

$value = "changed";

2 Answers 2

2

here is the code to open a php file using php :

$file = "/home/dir/file.php";
$fs = fopen( $file, "a+" ) or die("error when opening the file");
while (!feof($fs)) {
$contents .= fgets($fs, 1024);
}
fclose($fs);

now you can take the $contents and modify it to however you would like and then save it. here is how you can save it :

$fs = fopen( $_POST["file"], "a+" ) or die("error when opening the file");

fwrite($fs, $updatedContents);

fclose();

$updatedContents is the updated content

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, I would appreciate it you could choose my answer and/or vote up if it helped you solve your problem
Self-modifying code has its own set of problems, and unless you're ready to solve them you should avoid it.
I completely agree with Ignacio, however the OP asked to self-modify the code and the code above does that, but you are right, its better if it can be avoided.
what if the file is say, a config-file?
0

No you don't. Put the value in an external source, either a flat file or a database, and read in the value in a.php.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.