0

I want to get the root directory folder name in php. I know its possible to get the root directory path using _DIR_ but i need the folder name. Do we have a php function that can do this? In case the solution is using regular expressions on directory path, is it compatible in all system environments? eg windows, mac, etc.. Basically i dont want to get C:\xampp\htdocs\myProject\ i want myProject. Here is what i have tried $rootDirectoryPath= _DIR_; $rootDirectoryFoldername='';

5
  • have you tried this: php.net/manual/en/function.getcwd.php? Commented Oct 27, 2020 at 10:08
  • @DmytroHuz getcwd() return the same path as DIR Commented Oct 27, 2020 at 10:11
  • This is almost certainly a duplicate. Please continue researching. Commented Oct 27, 2020 at 10:18
  • check whether it works in windows or not basename(getcwd()) Commented Oct 27, 2020 at 10:20
  • AFAIK, there isn't any built in function to get the directory of the root path because you can have really different architecture depending of how you built your application. you have to define it yourself. But if it's always as you define it you can probably doing it using __DIR__ with an explode and get the last element with the DIRECTORY_SEPARATOR as delimiter value to make it works on all file system. Commented Oct 27, 2020 at 10:20

2 Answers 2

3

try this,

$projectFolderName = explode('/', $_SERVER['PHP_SELF'])[1];

the $_SERVER['PHP_SELF'] returns path to current file relative to your root/public folder of your server. You can then split the string on / which returns array of strings. First element of that array is the name of your project folder.

When using on different operating systems, You can check presence of / or \ and determine delimiter to use to explode.

$delimiter = strpos($_SERVER['PHP_SELF'], '\\') ? '\\' : '/';
$projectFolderName = explode($delimiter, $_SERVER['PHP_SELF'])[1];

This is one way to do it.

Sign up to request clarification or add additional context in comments.

4 Comments

will it work for C:\xampp\htdocs\myProject case ?
in your case the directory separator is '\', so try explode function with '\'. You'll have to use '\\' as back slash is used to escape quotes.
so your answer's code is not working in windows but the asker wants a solution for both case or any environment
I thought he wanted to get the name of project folder. Anyways, to make it work on different systems, there are number of ways you can try this, either check presence of \ or / and explode string depending on that, or if you are using some kind of environment variables file, then add a directory_separator variable there.
0

you can also use getcwd() with DIRECTORY_SEPARATOR

$folderName = substr(strrchr(getcwd(),DIRECTORY_SEPARATOR),1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.