Vivid Lust Posted February 14, 2009 Share Posted February 14, 2009 Hi all, Im wondering how I can write a PHP file with PHP?? Its because Im making an install program where the database details are put into the Config.php file. Any ideas please? Thanks lots!!! Quote Link to comment Share on other sites More sharing options...
nicholasstephan Posted February 14, 2009 Share Posted February 14, 2009 If you're looking to have a 1-time install function that writes a php file, take a look at: http://us.php.net/manual/en/ref.filesystem.php Have your system check for the existence of a config.php file. If it's not found they're redirected to the installation page, which builds and saves that config.php, and redirects them back to where they started. If you're looking to execute code dynamically, ie: pull some code out of a database and execute it, take a look at the eval() function. Hope this helps... cheers Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted February 14, 2009 Author Share Posted February 14, 2009 Could anyone give me an abriged example of building a config file (I've done the form which then checks the connection,) just dont know how to get the POST data into a file which has the following in: <?php $dbhost = $dbuser = $dbpass = $dbname = $tbl_name = ?> Thanks Quote Link to comment Share on other sites More sharing options...
drisate Posted February 14, 2009 Share Posted February 14, 2009 fopen and fwrite ;-) The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file. Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file. PHP Code: $ourFileName = "config.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code. Now we can use the fwrite command to add data to our file. We would do this as shown below: <?php $File = "YourFile.txt"; $Handle = fopen($File, 'w'); $Data = "Jane Doe\n"; fwrite($Handle, $Data); $Data = "Bilbo Jones\n"; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); ?> Quote Link to comment Share on other sites More sharing options...
9three Posted February 14, 2009 Share Posted February 14, 2009 Wit fopen and fwrite ;-) The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file. Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file. PHP Code: $ourFileName = "config.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code. Now we can use the fwrite command to add data to our file. We would do this as shown below: <?php $File = "YourFile.txt"; $Handle = fopen($File, 'w'); $Data = "Jane Doe\n"; fwrite($Handle, $Data); $Data = "Bilbo Jones\n"; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); ?> With that added. Now all you need to do is include the config.php into where ever it's suppose to go. Reminder: Becuase the config.php will not exist and/or not have the proper variables assigned, you need to create a check, otherwise you will run into some errors. Quote Link to comment Share on other sites More sharing options...
drisate Posted February 14, 2009 Share Posted February 14, 2009 Wit fopen and fwrite ;-) The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file. Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file. PHP Code: $ourFileName = "config.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fclose($ourFileHandle); The file "config.php" should be created in the same directory where this PHP code resides. PHP will see that "config.php" does not exist and will create it after running this code. Now we can use the fwrite command to add data to our file. We would do this as shown below: <?php $File = "YourFile.txt"; $Handle = fopen($File, 'w'); $Data = "Jane Doe\n"; fwrite($Handle, $Data); $Data = "Bilbo Jones\n"; fwrite($Handle, $Data); print "Data Written"; fclose($Handle); ?> With that added. Now all you need to do is include the config.php into where ever it's suppose to go. Reminder: Becuase the config.php will not exist and/or not have the proper variables assigned, you need to create a check, otherwise you will run into some errors. and you can do that like this <?php $filename = '/pathto/config.php'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted February 14, 2009 Author Share Posted February 14, 2009 so how would I add this to the config file for example: <?php $foo = $bar; ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 14, 2009 Share Posted February 14, 2009 Hey bro are you kidding me? I just told you how ... and it even been quoted 2 times ... <?php $File = "config.php"; $Handle = fopen($File, 'w'); $Data = '$foo = '.$bar.'; \n'; print "Data Written"; fclose($Handle); ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted February 14, 2009 Share Posted February 14, 2009 <?php $writeTo = "<?php\n\$foo = \$bar;\n?>"; $fh = fopen('config.php', "w+"); fwrite($fh, $writeTo); fclose($fh); ?> Simple as that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.