Creating an installer script
I'm trying to create an installer, using a combination of fwrite and forms. Here's my code:
<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, "$host = " . $_POST['host'] . ";\n $username = " . $_POST['username'] . ";\n $password = " . $_POST['password'] . ";\n $name = " . $_POST['name']";" ;);
fwrite($fh, 'mysql_connect("{$host}", "{$db_username}", "{$db_password}")\n or die(mysql_error());\n mysql_select_db("{$db_name}")\n or 开发者_JAVA百科die(mysql_error()); ?>' ;);
fclose($fh);
?>
Here's the error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home2/runetyco/public_html/ballpointradio/new/install_action.php on line 4
You are missing a .
at the end of the line
[...]. $_POST['name'] . [<--] ";" ;)
quite a few problems tbh.. sorted
<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, '<?php' . PHP_EOL . '$host = "' . $_POST['host'] . '";' . PHP_EOL . '$username = "' . $_POST['username'] . '";' . PHP_EOL . '$password = "' . $_POST['password'] . '";' . PHP_EOL . '$name = "' . $_POST['name'] . '";' . PHP_EOL );
fwrite($fh, 'mysql_connect($host, $db_username, $db_password)' . PHP_EOL . 'or die(mysql_error());' . PHP_EOL . 'mysql_select_db($db_name) or die(mysql_error());' . PHP_EOL);
fclose($fh);
note: i wouldn't recommend doing it this way, typically much better to only write out a simple config file and keep all code static, but the above answers your question about the error you get.
problem might be in ?>
. you'll have to escape it. also, you are not putting <?php
in the beginning of your conf file.
why do you want it to be php file anyway. why not use ini file and parse_ini_file(), or xml maybe?
The reason it's giving an error is because it's looking for the variable $host on line 4 - if you change the double quotes to single it should fix the problem - see below:
<?php
$myFile = "db_config.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, '$host = ' . $_POST['host'] . ';\n $username = ' . $_POST['username'] . ';\n $password = ' . $_POST['password'] . ';\n $name = ' . $_POST['name'] . ';\n\n';
fwrite($fh, 'mysql_connect("{$host}", "{$db_username}", "{$db_password}")\n or die(mysql_error());\n mysql_select_db("{$db_name}")\n or die(mysql_error()); ?>' ;);
fclose($fh);
?>
精彩评论