PDA

View Full Version : small snag


Creative Insanity
10-29-2005, 06:46 PM
Ok, more on the installer.

To make life easier for people installing sites I am making an installer and most of it is done, but hit a small wall when doing the following.

I have a connection script and table creation script which all work fine, but I want to enter that info for the connection script from a form.

$dbserver = "localhost";
$dbuser = "from form";
$dbpass = "from form";
$dbname = "from form";

I want the info entered into the form to go where is says 'from form'.
But since I have never written to a file before how would I go about this?

anyone have any ideas?

davidj
10-31-2005, 08:34 AM
i have written this

where i have \"from form1\". Replace this with a POST or GET method variable depending on your requirments

The script will write to a file called Connections.php. Make sure it exists. Also check the file permissions on the server. I had to chmod to 777 to get this to work.


<?php
$lb = "\n"; // line break
$dbserver1 = '$'."dbserver = \"localhost\"".$lb; // $_POST[ variable + linebreak
$dbuser1 = '$'."dbuser = \"from form1\"".$lb; // $_POST[ variable + linebreak
$dbpass1 = '$'."dbpass = \"from form2\"".$lb; // $_POST[ variable + linebreak
$dbname1 = '$'."dbname = \"from form3\"".$lb; // $_POST[ variable + linebreak

$data = $dbserver1.$dbuser1.$dbpass1.$dbname1;

$file = "Connection.php"; //create your Connections file and set path here (path/connections.php)
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }
echo "You have successfully set your data to $file";
fclose($file_handle);
?>

Creative Insanity
10-31-2005, 09:14 AM
Hey, thanks a million dj.. I am going to try this out in the morning.
Looks like just what I need actually :)
Let you know how it all goes.
If this does the trick that is another job out of the way and I will have a web based browser site installer. Cool.

davidj
10-31-2005, 02:49 PM
quick note creative..

i forgot to add the hardcoded semi colons to the variable txt lines

add these either by inserting into the begining of the linebreak variable or concatenating them at the end of the $dbserver variables

e.g...

$lb = ";\n";

or

$dbserver1 = '$'."dbserver = \"localhost\"".';'.$lb; // $_POST[ variable + linebreak

test first as im just guessing both will be valid

Creative Insanity
10-31-2005, 04:50 PM
Ok.. have added that also to the txt file.
Thanks again

Update

Ok have setup that file and at firsy try I got.

You have successfully set your data to connection.php

but when I looked at the connection file I had


$dbserver = ""; //MySQL server (localhost whould be correct)
$dbuser = ""; // MySQL user name
$dbpass = ""; // User password
$dbname = ""; // database name

:(
bummer.. back to the drawing board.

The part I do not understand is how does the file you showed me know where to put the data in the connection script?
Is something needed there? a session or something?

davidj
11-01-2005, 08:04 AM
i had a bit more time to look at this

i have included a form with hardcoded values (which you can change)
This is a form example but you could change it to suit any variable

I have commented out stuff to help

paste into page and call the page writefile2.php or change the form action value to what ever you want


<?php
$dbserver = $_POST['dbserver']; // gets value from field
$dbuser = $_POST['dbuser']; // gets value from field
$dbpass = $_POST['dbpass']; // gets value from field
$dbname = $_POST['dbname']; // gets value from field


// this checks that all fields have some data before writing to file.

if ($dbserver !='' && $dbuser !='' && $dbpass !='' && $dbname !='' ){

$lb = ";\n"; // line break \n is written which drops next line down
$phptag = '<?php'; //opening php tag set to variable (i forgot this before)
$phpendtag = '?>'; // closing php tag set to variable (i forgot this before)


// set field vars and turn into php
$dbserver1 = '$'."dbserver = \"$dbserver\"".$lb; // $_POST[ variable + linebreak
$dbuser1 = '$'."dbuser = \"$dbuser\"".$lb; // $_POST[ variable + linebreak
$dbpass1 = '$'."dbpass = \"$dbpass\"".$lb; // $_POST[ variable + linebreak
$dbname1 = '$'."dbname = \"$dbname\"".$lb; // $_POST[ variable + linebreak

//concat vars above into correct format
$data = $phptag." \n".$dbserver1.$dbuser1.$dbpass1.$dbname1.$phpendt ag;

//write the file
$file = "Connection.php"; //create your Connections file and set path here (path/connections.php)
if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; }
if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; }
echo "You have successfully set your data to $file";
fclose($file_handle);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="writefile2.php">
<p>
<input name="dbserver" type="text" id="dbserver" />
</p>
<p>
<input name="dbuser" type="text" id="dbuser" />
</p>
<p>
<input name="dbpass" type="text" id="dbpass" />
</p>
<p>
<input name="dbname" type="text" id="dbname" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>

</body>
</html>

Creative Insanity
11-01-2005, 09:12 AM
Hey david.. thanks for the time you put into this.. great stuff and will have a look at this in a sec.
I am a wizz with sql stuff.. but writing to another file I am a real dummy.

Update

Ok I now have a fully working installer.
I modified the file you posted dj to suit what I wanted and it works a treat.
Only have one more file to complete for the installer and that has nothing major in it and then all done.

Once again thanks a million for the help with this little problem dj.

I am now a wizz writing to files hehe.