4

In Laravel sometime I uptade environment values with sed with:

sed -i '/^MAIL_DRIVER=/s/=.*/=log/' .env

Can anyone propose and alternative to do this with PHP?

UPDATE: I used Laravel/dotenv .env file as a example to give some context to the question but I'm not interested in how to change enviroment variables programatically my interest is how to perform sed "like" operations with php file manipulation function or if exists some library to do that. I also know i could use system o passthru functions, that's what I'm already doing I just curios about how to manipulate files in this way using PHP

miken32
  • 42,008
  • 16
  • 111
  • 154

2 Answers2

1

While Laravel uses the dotenv library to read, it offers no way to programatically update the .env file. You can however update the configuration value using preg_replace. Here's a one line solution:

file_put_contents(base_path('.env'), preg_replace("/(MAIL_DRIVER)=(.*)/", "$1=log", file_get_contents(base_path('.env'))));

Here's also the solution expanded into multiple lines and explained:

// Read the .env file contents
$env = file_get_contents(base_path('.env'));

// Replace the value using regex matching
$env = preg_replace("/(MAIL_DRIVER)=(.*)/", "$1=log", $env);

// Write the updated contents to the file
file_put_contents(base_path('.env'), $env);
Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • This is just *wrong* - the getenv() and putenv() functions are used for this exact purpose. – kalatabe Feb 10 '16 at 13:51
  • @KaloyanDoichinov Please see my comment on your answer for clarification. – Bogdan Feb 10 '16 at 13:54
  • I understand, but OP says "I update environment values", my presumption was that they didn't know of a different way to change them. Maybe I'm wrong though, this is worth clarifying. – kalatabe Feb 10 '16 at 13:56
  • @KaloyanDoichinov The `sed` command updates the file contents, there's no question about that. So it is pretty clear that the intent is to modify the file. – Bogdan Feb 10 '16 at 13:58
  • I know what `sed` does, I meant that OP could be (mis)using it because he/she didn't know about the existence of `putenv`. Anyway there are now 2 answers, both of which will work for specific purposes. – kalatabe Feb 10 '16 at 14:02
  • I agree, both answers solve the two possible sides of the issue. But the down vote and initial comment were a bit impulsive and harsh on your part. – Bogdan Feb 10 '16 at 14:06
  • @Bogdan for my your answer is a good response (I vote this one but sorry I don't have enough reputation). What strikes me is that it doesn't exists any library to help doing manipulation like this in (config) files – Sergi Tur Badenas Feb 11 '16 at 10:28
  • @SergiTurBadenas You can still [mark this answer as the accepted one](http://meta.stackexchange.com/a/5235/199431) to let future readers know that this is a correct answer, because the useless down vote it has now makes it look like a wrong answer. – Bogdan Feb 11 '16 at 11:05
  • @Bogdan +1 for strategy to write changes to config file. I was looking for this exact solution. This works if you want to automate changes to .env or config files on-the-fly after a deployment. One example is for automated installation in different deployment environments where an 'enabled' value can be disabled if a resource referred to in a config key is unavailable. – windsor Feb 20 '18 at 19:09
0

You don't need a system call to do achieve this. You should use putenv()

putenv('MAIL_DRIVER=/log');

kalatabe
  • 2,909
  • 2
  • 15
  • 24
  • The OP wants to update the `.env` file contents (the `sed` command will modify the contents of the file) and your solution only updates the value temporarily in memory. As the `putenv` documentation states: **"The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state."**. – Bogdan Feb 10 '16 at 13:52