How do you get the current directory name in PHP?
I ran into a situation where I wanted to get the name of the directory I was in, in PHP. To be clear, I didn’t want the full path, just the directory/folder name
To be clear, if I was working in the directory:
I wanted to have addonname returned.
I worked out two solutions.
The first solution used the getcwd() function which returns the full directory path as shown above but to use the basename function to get the part I needed. It worked so… “echo basename(getcwd());” returns “addonname” in this scenario.
That would meet my needs perfectly. I believe that matches my requirements in spirit, but was not literally correct. That statement returned the current working directory, but not the directory where the file was located. In fact, PHP 4 and PHP 5 differ when getcwd() is called from the command line. If you are in the ‘/’ directory and execute “php /test/talktome.php“, a php 4 getcwd() in that file will return the path “/test” while PHP 5 will correctly return ‘/’.
To resolve this, this next call works even better:
Does anyone want to do some speed tests to see which is faster after 10,000 calls? Let us know your results.
thanks man it,s help me a lot