How to get current directory name in PHP

8

I searched Google and here in SOpt, but I did not find any sites, or ask here to answer this question.

Example:

I'm on page index.php which is in /5/index.php , then only 5 should be caught.

How to print this on the page?

  

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

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

    
asked by anonymous 20.12.2016 / 06:48

1 answer

9

The method for finding the current board is getcwd()

  

"gets the current working directory"

You have more alternatives,

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

  • You can use basename() in combination with __DIR__ that will give the name of the board you are in.

An example on PHP fiddle would look like this:

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

It gives:

/home/xfiddlec/public_html/main
/home/xfiddlec/public_html/main
main
    
20.12.2016 / 06:50