I have not long had to make one, So i will be going through the basic procedure of writing a database configuration file through php.
NOTE: This will note be the full outfit, as A. i dont have the time currently to do this, and it is really only a basic form submission like anything else.
If you dont know how to proccess a form submission, then you really are NOT at the point of needing to create an installer!
But this tutorial will cover the file creation.
So let us presume you have a form where a user inputs their database information. It is at the point of being parsed and tested. Everything is AOK and variables are defined...
$db_host = 'localhost'; $db_user = 'root'; $db_pass = 'password'; $db_table = 'table';we want to create a file which will contain something that resembles the following...
<?php $db_host = "localhost"; $db_user = "root"; $db_pass = "password"; $db_name = "table"; $dba = new mysqli($db_host, $db_user, $db_pass, $db_name); define("INSTALLED", 1, true); ?>This is a basic database configuration file that you will find in places like phpBB.
So how do we create said file? Well it is a simple process of writing to a file.
We need to first define the target file.
$target_file = '../scripts/db_config.php';Simple...
Now what we want to do is create a handle and open the file. It doesnt matter if this file physically exists, because if it doesn't, it will be created for us. we will achive this by using the functon fopen with its I/O mode set to 'w'. This will erase anything contained in the file, making it a fresh blank file to work with.
Now we need to define what we want to contain inside the newly opened file.
Well we define a new variable called $contents. We will define this variable with the same layout and structure we would as if we were to write the file in notepad.
$content = '<?php $db_host = "' . $db_host . '"; $db_user = "' . $db_user . '"; $db_pass = "' . $db_password . '"; $db_name = "' . $db_name . '"; $dba = new mysqli($db_host, $db_user, $db_pass, $db_name); define("INSTALLED", 1, true); ?> ';Now we have written this inside a string escaping only to place in dynamic variable information. One thing to make sure of, make sure there is NO white space between your opening quote and the first php tag.
The final part is simply to write the file to the system and then close the file. We will be using our $handle variable for both of these and the $content variable when we write.
The 2 methods we want to use are fwrite, in which we supply the handle and the content variables. The second is the fclose function in which we pass through the handle variable.
So...
fwrite($handle, $content); fclose($handle);Sorted, we have now successfully written our new db_config.php file which will connect us to mysql databse using database information supplied by the end user.
Now let me just explain the 'define('INSTALLED', 1);' i have added.
In my system, i run a check to first see if the file exsists, if it does, we include it. I also make a defined check to make sure that the INSTALLED constant has been made. without the script the define wont happen, if we try to include a file which isnt there, we get an error. So to run this smoothly, i have done the following.
///// CALL DATABASE SETUP if(file_exists($root_dir . "scripts/db_config.php")) include ($root_dir . "scripts/db_config.php"); ////// FORCE TO INSTALL IF INSTALL NOT DEFINED if(!defined('INSTALLED')) header("Location: install/");if the constant INSTALLED hasn't been defined, it means the config file has not been created, so we want to force the user to the install screen.
Hope that explains everything nice and easy for anyone who may need the code, either for an instillation script, or any kind of file you may need to write.