How to get current directory name in PHP

Asked

Viewed 5,548 times

8

I Googled and here on Sopt, but I did not find any site, or question here that would answer this question.

Example:

I’m on the page index.php who is in /5/index.php, then only the 5.

How to print this on the page?

What is the simplest and most practical way to get the directory name in PHP ?

Very simple question, but I did not find any useful method to get this information. : P

2 answers

10


The method for knowing the current board is getcwd()

"gets the Current Working directory"

You have more options,

  • you can use the dirname in combination with __FILE__ which gives the directory of the file you are in.

  • you can use the basename() in combination with the __DIR__ who will name the board you’re on.

An example in PHP fiddle would be so:

echo getcwd().'<br>';
echo dirname(__FILE__).'<br>';
echo basename(__DIR__).'<br>';

that gives:

/home/xfiddlec/public_html/main
/home/xfiddlec/public_html/main
main
  • could you give an example of use? : P

  • 1

    @Alexandrelopes joined example

  • 2

    What a beautiful answer! Kkkk, it’s perfect! So that’s it basename(__DIR__), Thank you @Sergio helping me always! : P

-4

echo basename($_SERVER['REQUEST_URI']);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.