Forum Moderators: coopster

Message Too Old, No Replies

PHP Writing to a file help

         

adammc

12:26 am on Jun 1, 2007 (gmt 0)

10+ Year Member



Hi folks,

I am trying to create a script that will take the MYSQl DB details that have been sent from a form and store it in a file for usage at a later stage.

Am I doing this correctly?

[php]
$File = "db-settings.php";
$Handle = fopen($File, 'w');

$Data = "$dbuser = $_POST['dbuser'];\n";
fwrite($Handle, $Data);

$Data = "$dbpass = $_POST['dbpass'];\n";
fwrite($Handle, $Data);

$Data = "$db = $_POST['db'];\n";
fwrite($Handle, $Data);

$Data = "$host = $_POST['host'];\n";
fwrite($Handle, $Data);

print "Data Written";
fclose($Handle);
[/php]

Any advice would be greatly appreciated :)

adammc

12:45 am on Jun 1, 2007 (gmt 0)

10+ Year Member



after escaping the '$Data....

I have added this for error checking:
[php]

if(!$Handle) {
echo 'Error, the file could not be opened or there was an error creating it.';
exit;
}
[/php]

Should I be checking anytinhg else?

jatar_k

1:10 am on Jun 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Am I doing this correctly?

you're going to hate this answer

is it working? if it's working then you did it. Or are you concerned with some specific thing

if(!$Handle) {

it's funny, though I know all the docs say to do this, and it's right, the only time this matters is when you are reading only, the rest of the time it tries to create the file anyway, so if the dir doesn't exist then it's fine I guess, it's just a weird thing.

>> check anything else

make sure the file pointer was created, as you did, you should check all those POST vars.

adammc

1:44 am on Jun 1, 2007 (gmt 0)

10+ Year Member



hello & thank you jatar_k :)

"you should check all those POST vars. "
Do you mean validate them to ensure they are correct form / format?

jatar_k

1:48 am on Jun 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



and make sure there are no bad characters in there. db names have a set of allowable chars
[dev.mysql.com...]

hard to say how much you need to do since I am not totally sure what this could be used for

adammc

2:09 am on Jun 1, 2007 (gmt 0)

10+ Year Member



I'm creating an online client invoicing / billing system.

I'm designing it with install files etc so that I may be able to sell the script at a later stage.

Is there a way to check that data was actually written?

jatar_k

11:51 am on Jun 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can check the return from fwrite

fwrite() returns the number of bytes written, or FALSE on error.