Php current directory of file

I have a php page inside a folder on my website.

I need to add the name of the current directory into a variable for example:

$myVar = current_directory_name;

Is this possible?

hakre

187k48 gold badges419 silver badges803 bronze badges

asked Apr 3, 2012 at 16:20

Satch3000Satch3000

45.1k86 gold badges211 silver badges345 bronze badges

getcwd[];

or

dirname[__FILE__];

or [PHP5]

basename[__DIR__] 

//php.net/manual/en/function.getcwd.php

//php.net/manual/en/function.dirname.php

You can use basename[] to get the trailing part of the path :]

In your case, I'd say you are most likely looking to use getcwd[], dirname[__FILE__] is more useful when you have a file that needs to include another library and is included in another library.

Eg:

main.php
libs/common.php
libs/images/editor.php

In your common.php you need to use functions in editor.php, so you use

common.php:

require_once dirname[__FILE__] . '/images/editor.php';

main.php:

require_once libs/common.php

That way when common.php is require'd in main.php, the call of require_once in common.php will correctly includes editor.php in images/editor.php instead of trying to look in current directory where main.php is run.

Oliver Nybo

5501 gold badge8 silver badges22 bronze badges

answered Apr 3, 2012 at 16:21

Andreas WongAndreas Wong

58.5k19 gold badges105 silver badges123 bronze badges

5

To get only the name of the directory where script executed:

//Path to script: /data/html/cars/index.php
echo basename[dirname[__FILE__]]; //"cars"

answered Aug 1, 2014 at 5:31

You can use dirname[__FILE__] to get the path to the directory of the current file.

Example: /path_to/your_dir/your_file.php:

// use dirname to get the directory of the current file
$path = dirname[__FILE__];
// $path here is now /path_to/your_dir

// split directory into array of pieces
$pieces = explode[DIRECTORY_SEPARATOR, $path];
// $pieces = ['path_to', 'your_dir']

// get the last piece
echo $pieces[count[$pieces] - 1];
// result is: your_dir

George

2,78217 silver badges29 bronze badges

answered Mar 14, 2013 at 10:35

2

echo basename[__DIR__]; will return the current directory name only
echo basename[__FILE__]; will return the current file name only

answered Sep 28, 2013 at 19:03

1

Actually I found the best solution is the following:

$cur_dir = explode['\\', getcwd[]];
echo $cur_dir[count[$cur_dir]-1];

if your dir is www\var\path\ Current_Path

then this returns Current_path

answered Mar 24, 2013 at 1:42

AndrewAndrew

711 silver badge1 bronze badge

1

$myVar = str_replace['/', '', $_SERVER[REQUEST_URI]];

libs/images/index.php
Result: images

Oliver Nybo

5501 gold badge8 silver badges22 bronze badges

answered Aug 16, 2016 at 0:28

MickyMicky

1073 bronze badges

0

To get the names of current directory we can use getcwd[] or dirname[__FILE__] but getcwd[] and dirname[__FILE__] are not synonymous. They do exactly what their names are. If your code is running by referring a class in another file which exists in some other directory then these both methods will return different results.

For example if I am calling a class, from where these two functions are invoked and the class exists in some /controller/goodclass.php from /index.php then getcwd[] will return '/ and dirname[__FILE__] will return /controller.

answered Jan 1, 2021 at 4:32

I use this line to get the actual file directory name without the path. [For windows]

substr[dirname[__FILE__], strrpos[dirname[__FILE__], '\\'] + 1]

I changed it a bit to work on Linux also

substr[dirname[__FILE__], strrpos[str_replace['\\', '/', dirname[__FILE__]], '/'] + 1]

answered Sep 30, 2021 at 8:44

basename[getcwd[]]; // returns only the current working dir name.

answered Mar 4 at 9:06

1

How can get current working directory in PHP?

Use the getcwd[] Function to Get the Current Directory Name in PHP. The getcwd[] function gives the current working directory. The returned value is a string on success. The function does not take any parameters.

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.

How do I view a directory in PHP?

PHP scandir[] Function $b = scandir[$dir,1];

What is dirname [__ file __] in PHP?

dirname[__FILE__] allows you to get an absolute path [and thus avoid an include path search] without relying on the working directory being the directory in which bootstrap. php resides. [Note: since PHP 5.3, you can use __DIR__ in place of dirname[__FILE__] .]

Chủ Đề