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.
Add a comment
|
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 usedirname($initial_called_file)
. Also, Brad appears to have been seduced by snake case. – Stephen Lewis 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