Skip to content
Advertisement

str_replace() is not working in aws server returning empty string but working on cpanel and locally

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.

  1. The First point is – your /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.
  2. The Second point is – You are using laravel config and your $searchfor variable coming from bootstrap/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.
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement