Search code examples
phpbraintree-sandbox

Installing Braintree and getting error "Uncaught InvalidArgumentException: Dotenv: Environment file .env not found or not readable."


I'm trying to get Braintree working on my local Ubuntu environment and I'm getting the error Uncaught InvalidArgumentException: Dotenv: Environment file .env not found or not readable. I think its relating to the backslash in the code below. I'm not sure how to fix it though, can anyone help with this?

This is the error I'm receiving:

[Fri Apr 05 14:14:26.170718 2019] [php7:error] [pid 88156] [client ::1:52042] PHP Fatal error:  Uncaught InvalidArgumentException: Dotenv: Environment file .env not found or not readable. Create file with your environment settings at /var/www/html/myskinpro/includes/../example.env/.env in /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php:78\nStack trace:\n#0 /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php(51): Dotenv\\Loader->ensureFileIsReadable()\n#1 /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Dotenv.php(41): Dotenv\\Loader->load()\n#2 /var/www/html/myskinpro/includes/braintree_init.php(7): Dotenv\\Dotenv->load()\n#3 /var/www/html/myskinpro/payusd/index.php(1): require_once('/var/www/html/m...')\n#4 {main}\n  thrown in /var/www/html/myskinpro/vendor/vlucas/phpdotenv/src/Loader.php on line 78

This is the code that's causing the problem:

<?php
session_start();
require_once("../vendor/autoload.php");

if(file_exists(__DIR__ . "/../example.env")) {
    $dotenv = new Dotenv\Dotenv(__DIR__ . "/../example.env");
    $dotenv->load();
} else {
  echo "file does not exist";
}

$gateway = new Braintree\Gateway([
    'environment' => getenv('BT_ENVIRONMENT'),
    'merchantId' => getenv('BT_MERCHANT_ID'),
    'publicKey' => getenv('BT_PUBLIC_KEY'),
    'privateKey' => getenv('BT_PRIVATE_KEY')
]);

Solution

  • This code fixes the issue:

    if(file_exists(__DIR__ . "/../example.env")) {
        $dotenv = new Dotenv\Dotenv(__DIR__ . "/../", 'example.env');
        $dotenv->load();
    } else {
      echo "file does not exist";
    }