Tousif Ali Tousif Ali - 1 year ago 351
PHP Question

How to get root directory in yii2

yii2 Question

My yii2 install in


I want to get above path to save images which will be uploaded by me or users.

I have pass all available arguments in
(below are the lists of argument which I have used).

- framework directory.

- base path of currently running application.

- runtime directory.

- Composer vendor directory.

- web root directory of currently running web application.

- base URL of currently running web application.

And also once I will get above path to save images then how can I get path something like this
to be use in img tag src.

One more thing how can I create my own alias with
and where to create this so that I can load it on every controller.

Answer Source

Open file D:\wamp\www\yiistore2\common\config\params-local.php

Paste below code before return

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

After inserting above code in params-local.php file your file should look like this.

Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));

return [

Now to get path of your root (in my case its D:\wamp\www\yiistore2) directory you can use below code in any php file.

echo Yii::getAlias('@anyname');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download