How might I open a file fopen()

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
Hello.

Apologies if I've come to the wrong forum.

Basically, I have created an installation file which accesses a database which, then creates tables. (hence posting here :oops: )
After the tables have been created. I would like to write the Database's Name, Username, Password and website url to a file on the server.

Then, when I type in my site's url. I would like the index.php file to load the file, and store the Database's Name, Username, Password and website url in variables.

This is where I'm getting stuck. I've written the code to save first.:-

// Get variables from form and set in php
$hostname = $_POST["txtDatabase_Host"];
$username = $_POST["txtDatabase_Username"];
$password = $_POST["txtDatabase_Password"];
$database = $_POST["txtDatabase_Name"];
$site_url = $_POST["txtSite_Url"];

// Write database information to a file.
include 'http://www.mywebsite.com/Core/sys/beardbf.txt';

$fp = fopen("http://www.mywebsite.com/Core/sys/beardbf.txt", 'w' ) or die("Coudn't open file");
echo "Try";
fwrite($fp, "Hello World.");
fputs($fp, "Hello World.");

fwrite($fp, $hostname);
fputs($fp, $hostname);

fwrite($fp, $database);
fputs($fp, $database);

fwrite($fp, $username);
fputs($fp, $username);

fwrite($fp, $password);
fputs($fp, $password);


fclose( $fp );

echo "Check file".


I would like to ask if the above code looks correct, or if not, how should I go about it please?

Thank You.
 
fputs and fwrite are the same function, you are therefore duplicating your data with that script; use one or the other as it is much less confusing.

As to what you are doing, it sounds more complicated than it needs to be.

Are these separate servers or the same?

Are you going to be maintaining the database files - hence the reason for wanting a list of login details?
 
Hello Hyper,

Thank you for your reply.
In response, I'm only using 1 server. It's been quite a while since I used fputs and fwrite, therefore I was not sure which one to use.

Basically, I am trying to do is, write the database details to a file.
And later on read the file back if I need to.

I could not remember how to read & write, hence writing the code in my original post.

Thanks again.
 
Hello hyper,

The server was just reporting an error with the code line:-
$fp = fopen("http://www.mywebsite.com/Core/sys/beardbf.txt", 'w' ) or die("Coudn't open file");

But it's now reporting Coudn't open file message.
As the beardbf.txt and the original beardbf.php files do already exist, and the directories Core and sys and 2 files are chmod 777, I'm having to review if I've missed something.

Thank you again for your help It's much appreciated. :D
 
Hello Hyper,

Just an update :)

I've now managed to open a file on the server.

The code I had to use reads:-
$fp = fopen( dirname(__FILE__).'/../../../Core/sys/beardbf.txt', 'r' ) or die("Couldn't open file:- ");

__).'/../../../Core/sys/beardbf.txt' been the path to the file and the filename itself.

Thank you again for your replies.
 
Try doing this
Code:
$root = $_SERVER['DOCUMENT_ROOT'];

If you then
Code:
echo $root;
You'll get an idea for where the document root sits in relation to the rest of the file system.

Then you can use
Code:
"$root/Core/sys/beardbf.txt;"
Using double quotes.

instead of
Code:
/../../../Core/sys/beardbf.txt;
Then you don't have to worry about how many levels you are traversing since it will always be from your web-root 8)

I suspect the problem with accessing the file using http://... is that it is switched off in your php.ini file and not allowed?
 
Back
Top