Hi,
Could I please briefly enquire about reading and writing to a file using php?
I'm looking for a way of storing a database's, Name, Username and Password. Then reading it back again.
Writing to a file, I've written:-
// Write to file. $fp = fopen( dirname(__FILE__).'/sys/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, I've written:-
$fp = fopen( dirname(__FILE__).'/sys/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, I'm finding the data is been written in one long string (ie localhostusernamepassworddatabasename).
I'd just like to ask if it is possible read the file so that $db_host would hold just hostname, $db_user would hold just the database username and so on?
Also (out of interest) is there another way of reading and writing. I've thought about keeping each piece of data in a separate file.
Thank You.