Skip to content
Advertisement

PHP Get name of current directory

I have a php page inside a folder on my website.

I need to add the name of the current directory into a variable for example:

JavaScript

Is this possible?

Advertisement

Answer

JavaScript

or

JavaScript

or (PHP5)

JavaScript

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

You can use basename() to get the trailing part of the path 🙂

In your case, I’d say you are most likely looking to use getcwd(), dirname(__FILE__) is more useful when you have a file that needs to include another library and is included in another library.

Eg:

JavaScript

In your common.php you need to use functions in editor.php, so you use

common.php:

JavaScript

main.php:

JavaScript

That way when common.php is require'd in main.php, the call of require_once in common.php will correctly includes editor.php in images/editor.php instead of trying to look in current directory where main.php is run.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement