SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: PHP Writing to a file help
-
May 31, 2007, 17:26 #1
- Join Date
- Aug 2004
- Location
- Cairns, Australia
- Posts
- 762
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Writing to a file help
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 Code:$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);
-
May 31, 2007, 17:28 #2
- Join Date
- Jul 2006
- Location
- Kansas City, MO
- Posts
- 280
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You need to escape $dbuser and $dbpass and so forth. Otherwise you'll end up with:
= username
= password
= db
= host
see below.
-
May 31, 2007, 17:30 #3
- Join Date
- Jul 2006
- Location
- Kansas City, MO
- Posts
- 280
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$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);
-
May 31, 2007, 17:30 #4
- Join Date
- Jan 2007
- Location
- Belgium
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I prefer using an ini-file (not very Unix but still), protected by htaccess.
To read/write from and to ini-files I wrote a simple PHP class.
For example an ini-file would contain:
setting = value; #CommentsFOR SALE: 1 set of morals, never used, will sell cheap
-
May 31, 2007, 17:43 #5
- Join Date
- Aug 2004
- Location
- Cairns, Australia
- Posts
- 762
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ZareMedia,
Thanks for the great tip
I have added this for error checking:
PHP Code:
if(!$Handle) {
echo 'Error, the file could not be opened or there was an error creating it.';
exit;
}
-
May 31, 2007, 19:06 #6
- Join Date
- Aug 2004
- Location
- Cairns, Australia
- Posts
- 762
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is there a way to check that data was written?
-
May 31, 2007, 19:10 #7
- Join Date
- Feb 2005
- Location
- Brisbane, Australia
- Posts
- 306
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Look at the file?
Bookmarks