Jump to content

How do I read and write to file?


IChaps
 Share

Recommended Posts

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.

Edited by IChaps
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...