Hello.
I'd just like to enquire how I would read and write to a file using php please?
I'm looking to write a database's, Host, Username, Password, and database Name to a file name on the server, then at a later date. When I load the site, read this information back.
At present I'm using the following to write to the file:-
// Write to file. $fp = fopen( dirname(__FILE__).'/dbinfo.txt', 'w' ) or die("Couldn't open file:- "); fwrite($fp, $hostname); fwrite($fp, $database); fwrite($fp, $username); fwrite($fp, $password); fclose( $fp );
and to read back:-
$fp = fopen( dirname(__FILE__).'/dbinfo.txt', 'r' ) or die("Couldn't open file:- "); $db_host = fread($fp, $hostname); // $db_host $db_user = fread($fp, $username); // $db_user $db_pass = fread($fp, $password); // $db_pass $db_databasename = fread($fp, $database); fclose( $fp );
However, it reads it all in one long string, instead of it been on separate lines.
Would anyone know the correct way of writing this code, please?
Thank You.