<p>Lots of good discussion on how to do this and the pros/cons of each here: <a href="https://stackoverflow.com/questions/4645082/get-absolute-path-of-current-script">https://stackoverflow.com/questions/4645082/get-absolute-path-of-current-script</a></p>

<p>In short, the most reliable way is to use PHP's <a href="https://secure.php.net/manual/en/function.debug-backtrace.php" rel="noreferrer">debug_backtrace</a>.</p>

<pre><code>$debugTrace = debug_backtrace();
$initialCalledFile = count($debugTrace) ? $debugTrace[count($debugTrace) - 1]['file'] : __FILE__;
$publicFolderPath = dirname($initialCalledFile);
</code></pre>

CC BY-SA 3.0
2017-05-08T01:49:39.143Z
One small amendment: that will give you the path to the `index.php` file within the public directory. To get the path to the public directory, you need to use `dirname($initial_called_file)`. Also, Brad appears to have been seduced by snake case.
Stephen Lewis
CC BY-SA 3.0
2017-05-08T03:14:02.503Z
@StephenLewis copy/paste is the only way I know how to program. Updated answer.
Brad Bell
CC BY-SA 3.0
2017-05-07T23:26:41.857Z
2017-05-08T02:52:09.937Z
2017-05-08T02:52:09.937Z
Brad Bell
<p>Within my Craft plugin code, how do I find the path to Craft's <code>public/</code> folder? There doesn't seem to be a constant listed in <a href="https://craftcms.com/docs/php-constants" rel="noreferrer">Craft Docs: PHP Constants</a> like there is for all the other key folders.</p>

<plugin-development>
How do I find the path to the public folder?
# How do I find the path to the public folder?

(Asked by: username_0 on 2017-05-07)

Within my Craft plugin code, how do I find the path to Craft's `public/` folder? There doesn't seem to be a constant listed in [Craft Docs: PHP Constants](https://craftcms.com/docs/php-constants) like there is for all the other key folders.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-05-07. Score: 6

Lots of good discussion on how to do this and the pros/cons of each here: https://stackoverflow.com/questions/4645082/get-absolute-path-of-current-script
In short, the most reliable way is to use PHP's debug_backtrace.
$debugTrace = debug_backtrace();
$initialCalledFile = count($debugTrace) ? $debugTrace[count($debugTrace) - 1]['file'] : __FILE__;
$publicFolderPath = dirname($initialCalledFile);

> **Comments:**
> **username_2**: One small amendment: that will give you the path to the `index.php` file within the public directory. To get the path to the public directory, you need to use `dirname($initial_called_file)`. Also, Brad appears to have been seduced by snake case.  
> **username_1**: @StephenLewis copy/paste is the only way I know how to program. Updated answer.  

---

