Kyle Thomas Kyle Thomas - 1 month ago 7
PHP Question

fopen to add variable to file

I am creating an installer for my product that allows the developer to install the software. There is a configuration php file which I am opening in the same directory that the class exists which works fine.

class Package {
private $_config;
public function __construct($dsn,$user,$pass) {
$this->_config = fopen('Config.php', 'w') or die("Unable to find Config file");
[...]


However, when I want to then add the connection details to the file like so:

fwrite($this->_config, "$con = ['$dsn','$user','$pass'];");
fclose($this->_config);


the configuration file looks like this:

= ['val', 'val', 'val']; // where val is the actual value of $dsn, $user, $pass


How can I achieve the file to look like this?:

$con = ['val', 'val', 'val'];


Never really used a file to store information, only databases so I'm new to the concept.

Answer

Do not use double quotes --"--, because they evaluate "$con" to empty string Try this:

fwrite($this->_config, '$con' . " = ['$dsn','$user','$pass'];");
Comments