Skip to content

Instantly share code, notes, and snippets.

@d3v2a
Created September 26, 2016 13:21
Show Gist options
  • Save d3v2a/b5a8cf16b318f6c137d5f0b8a9a5abe3 to your computer and use it in GitHub Desktop.
Save d3v2a/b5a8cf16b318f6c137d5f0b8a9a5abe3 to your computer and use it in GitHub Desktop.
update env file with php
$key = $argv[1];
$value = $argv[2];
$path = dirname(__FILE__) . '/.env';
$newContent = '';
$content = fopen($path, 'r+');
$found = false;
while (!feof($content)) {
$line = fgets($content);
if (!empty(trim($line))) {
$param = explode('=', $line);
if ($param[0] == $key) {
$found = true;
$line = $param[0] . '=' . $value . PHP_EOL;
}
}
$newContent .= $line;
}
if (!$found) {
$newContent .= $key . '=' . $value . PHP_EOL;
}
ftruncate($content, 0);
rewind($content);
fwrite($content, $newContent);
fclose($content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment