I am developing a laravel ride sharing applicaiton and for the settings data I used a config files. To change any value usign file file_get_contents after that with str_replace and file_put_contents for updating the value.
here is some code example:
$config = base_path() . '/config/constants.php';
$file = file_get_contents($config);
$change_content = $file;
foreach ($request->all() as $key => $value) {
$value = (trim($value) == 'on') ? '1' : trim($value);
$searchfor = config('constants.' . $key);
if ($value != $searchfor) {
$search_text = "'" . $key . "' => '" . $searchfor . "'";
$value_text = "'" . $key . "' => '" . $value . "'";
$change_content = str_replace($search_text, $value_text, $change_content);
}
file_put_contents($config, $change_content);
}
But the $change_content doesn’t return any value in the AWS server
NOTE: This is working in my local machine also in Cpanel but in AWS ec2 not working. I am using php7.4, Nginx in Ubuntu 20.04. Same configuration for Cpanel, local, and AWS ec2.
Can anyone tell me that is there any extra configuration for str_replcae??
2
Answers
In your code, there can be 2 possible errors. As your code is running local and your files have write permissions.
/config/constants.php
file is too big that’s why the foreach loop is taken too much time and exit it but for this PHP should throw an error.$searchfor
variable coming frombootstrap/cache/config.php
. And you are replacing the value with the config cache value but maybe your/config/constants.php
doesn’t have this exact key-value or have some spaces between there. I think you got my point.Check these things are correct or not?
constants.php
has the same contentsphp artisan config:cache
this command re-caches your config again.