how to update a php file's source code via another php file
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:
$开发者_JS百科value = "changed";
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
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
.
精彩评论