Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-08T01:49:39.143Z
Answers.Text: 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.
Answers.UserDisplayName: Stephen Lewis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-08T03:14:02.503Z
Answers.Text: @StephenLewis copy/paste is the only way I know how to program. Updated answer.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T23:26:41.857Z
Answers.LastActivityDate: 2017-05-08T02:52:09.937Z
Answers.LastEditDate: 2017-05-08T02:52:09.937Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development>
Title: How do I find the path to the public folder?
ThreadText: # 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](https://secure.php.net/manual/en/function.debug-backtrace.php).
  
  ```
  $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, username_1 appears to have been seduced by snake case.  
  > **username_1**: @username_2 copy/paste is the only way I know how to program. Updated answer.  
  
  ---
  
  