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 like there is for all the other key folders.


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);
  • 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. May 8 '17 at 1:49
  • @StephenLewis copy/paste is the only way I know how to program. Updated answer.
    – Brad Bell
    May 8 '17 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.