Created
September 26, 2016 13:21
-
-
Save d3v2a/b5a8cf16b318f6c137d5f0b8a9a5abe3 to your computer and use it in GitHub Desktop.
update env file with php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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