<p>Check out <a href="https://github.com/nystudio107/craft-multi-environment" rel="nofollow noreferrer">craft-multi-environment</a>; even if you don't use it, you might crib some of the ideas from it.</p>

<p>For instance, how the <a href="https://github.com/nystudio107/craft-multi-environment/blob/master/example.env.php#L49" rel="nofollow noreferrer">basePath is determined</a>.</p>

CC BY-SA 3.0
2017-02-22T06:55:08.700Z
2017-02-22T06:55:08.700Z
andrew.welch
<p>So in my general config file I've defined an environmental variable called basePath (as per the <a href="https://craftcms.com/docs/multi-environment-configs#environment-specific-variables" rel="nofollow noreferrer">Environment-Specific Variables documentation</a>). I'm using the <a href="http://php.net/manual/en/function.getcwd.php" rel="nofollow noreferrer">getcwd()</a> function to get the working directory.</p>

<pre><code>return array(
    '*' =&gt; array(
        'environmentVariables' =&gt; array(
            'basePath' =&gt; getcwd(),
        )
    ),
);
</code></pre>

<p>I'm using this for a few reasons:</p>

<ol>
<li><p>I work in an environment where many people can be working on the same project at the same time. So their local basePath is always different.</p></li>
<li><p>My different <a href="https://craftcms.com/docs/config-settings#siteUrl" rel="nofollow noreferrer">siteUrls</a> for my setup use trailing locales in the url: <code>'siteUrl' =&gt; array( 'en' =&gt; 'http://www.website.com/', 'fi' =&gt; 'http://www.website.com/fi/' )</code>. So when it comes to targeting environment-specific locales, things get messy.</p></li>
<li><p><code>$_SERVER['DOCUMENT_ROOT']</code> is ok locally, but fails on a live server.</p></li>
</ol>

<p><strong>So my problem is</strong> when using <code>getcwd()</code>, the locale is included in path. Is there a way to trim this out dynamically? </p>

<p>Or is there a way to strip out the current locale from this path on-the-fly? <code>rtrim(getcwd(),"fi")</code> </p>

<p>Maybe there is a more reliable way to get the working directory?</p>

<config><multi-environment><environmentvariables>
Define a dynamic 'basePath' environmental variable without the locale
# Define a dynamic 'basePath' environmental variable without the locale

(Asked by: username_0 on 2017-02-22)

So in my general config file I've defined an environmental variable called basePath (as per the Environment-Specific Variables documentation). I'm using the getcwd() function to get the working directory.
return array(
    '*' => array(
        'environmentVariables' => array(
            'basePath' => getcwd(),
        )
    ),
);

I'm using this for a few reasons:

I work in an environment where many people can be working on the same project at the same time. So their local basePath is always different.
My different siteUrls for my setup use trailing locales in the url: 'siteUrl' => array( 'en' => 'http://www.website.com/', 'fi' => 'http://www.website.com/fi/' ). So when it comes to targeting environment-specific locales, things get messy.
$_SERVER['DOCUMENT_ROOT'] is ok locally, but fails on a live server.

So my problem is when using getcwd(), the locale is included in path. Is there a way to trim this out dynamically? 
Or is there a way to strip out the current locale from this path on-the-fly? rtrim(getcwd(),"fi") 
Maybe there is a more reliable way to get the working directory?

**Tags:** config, multi-environment, environmentvariables


---

## 1 answers

### Answer by username_1 on 2017-02-22. Score: 1

Check out [craft-multi-environment](https://github.com/nystudio107/craft-multi-environment); even if you don't use it, you might crib some of the ideas from it.

For instance, how the [basePath is determined](https://github.com/nystudio107/craft-multi-environment/blob/master/example.env.php#L49).


---

