Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get just the name of the parent folder in the directory where a script is running in php?

Tags:

php

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.

like image 223
Moshe Avatar asked Jan 23 '12 23:01

Moshe


People also ask

How can I get parent directory in php?

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.

What is __ DIR __ in php?

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.

What is the name of parent folder?

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.


1 Answers

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.

like image 106
rdlowrey Avatar answered Sep 28 '22 02:09

rdlowrey