Zend 2:: getting public folder path or basePath() easily in controller action
Solution 1
index.php
sets the current working dir to you application root (the folder containing composer.json
, init_autoloader.php
, etc.)
As long as you haven't called chdir
elsewhere in your application you can call getcwd()
and it'll always return the path to your app root.
Since the public folder is relative to that, you can get the path using ...
$publicDir = getcwd() . '/public';
Solution 2
In your public folder edit your file named index.php add only two lines
define('BASE_PATH', realpath(dirname(__DIR__)));
define('PUBLIC_PATH', BASE_PATH.'/public');
you can use in your code like
print_r(BASE_PATH);
print_r(PUBLIC_PATH);
karim_fci
Passionate software developer to work with the state of the art technology.
Updated on June 17, 2022Comments
-
karim_fci almost 2 years
In my application, to move a file to a specific directory i need to know public folder path in controller action. I read different this type solution but not getting easy one. I know that in view we can get easily public folder path using
$this->basePath();
view helper. I exactly want this in controller action. Anybody can guide me how can i achieve that. Thanks in advance. -
sk001 over 5 yearsYou saved me. Thanks.