IChaps Posted October 23, 2016 Share Posted October 23, 2016 (edited) 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 October 23, 2016 by IChaps Link to comment Share on other sites More sharing options...
Ingolme Posted October 23, 2016 Share Posted October 23, 2016 You have to specifically write line breaks in the file. If you don't put a line break there, there won't be a line break. fwrite($fp, $hostname . "\n"); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now