I would like to get just the name of the parent folder of which a script is currently running in the directory?
if we have a script called foo.php with a path of "/Users/Somone/Sites/public/foo.php", how can i go about just getting "public"out of the that file path and not the entire directory tree?
any help would be great.
thanks.
If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.
The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
A folder located within another folder is called a sub-folder. The main folder is called the parent folder. Folders and sub-folders help us to keep our files neatly.
The simplest way to do it:
basename(__DIR__);
As @mario sagely noted, this is only possible with PHP 5.3+, so if you're stuck with 5.2 or less ... well ... you should switch to a new host and stop using legacy software.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With