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??
Advertisement
Answer
NOTE: No there is no extra addon/extension in PHP for str_replace. str_replace() is built in string function on PHP 4, PHP 5, PHP 7, PHP 8.
In your code, there can be 2 possible errors. As your code is running local and your files have write permissions.
- The First point is – your
/config/constants.phpfile 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. - The Second point is – You are using laravel config and your
$searchforvariable coming frombootstrap/cache/config.php. And you are replacing the value with the config cache value but maybe your/config/constants.phpdoesn’t have this exact key-value or have some spaces between there. I think you got my point.