Hướng dẫn get current path php

  1. HowTo
  2. PHP Howtos
  3. Get Current Directory Name and Path in PHP

Created: December-04, 2021

Nội dung chính

  • Use the getcwd() Function to Get the Current Directory Name in PHP
  • Use the dirname() Function to Get the Current Directory Name in PHP
  • Use the basename() Function to Get the Current Directory Name in PHP
  • Related Article - PHP File
  • Related Article - PHP Directory
  • Not the answer you're looking for? Browse other questions tagged php directory or ask your own question.

  1. Use the getcwd() Function to Get the Current Directory Name in PHP
  2. Use the dirname() Function to Get the Current Directory Name in PHP
  3. Use the basename() Function to Get the Current Directory Name in PHP

This article will introduce a few methods to get the current working directory name 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. The function returns false in case of failure.

Let’s consider the following directory structure.

├── var
│ └── www
│ └── html
| └──project
| └──index.php

The PHP file lies inside the project directory. The getcwd() function will return the name of the current working directory, which is project.

We can use the echo function to display the content of the function. We can see in the output section that the getcwd() function returns the current working directory with its path.

echo getcwd();

Output:

/var/www/html/project

Use the dirname() Function to Get the Current Directory Name in PHP

We can also use the dirname() function to get the current directory name in PHP. The function returns the path of the parent directory.

It accepts two parameters where the first one is the path and the second one is levels. Levels indicate the number of directories to move up.

Finally, we can use the __FILE__ magic constants in the dirname() function to get the name of the current directory. The __FILE__ constant returns the full path of the current file along with the file name.

We can demonstrate these constants and the function in the above directory structure. For example, we get the following result when we echo the __FILE__ constant from the index.php file.

echo __FILE__;

Output:

/var/www/html/project/index.php

For example, write the dirname() function in the index.php file with the __FILE__ constant as the parameter.

echo dirname(__FILE__);

Output:

/var/www/html/project

In this way, we can get the current working directory name in PHP.

Use the basename() Function to Get the Current Directory Name in PHP

We can use the basename() function to get the current working directory name without the path in PHP. We can apply this function with the result of the above two functions.

The basename() function returns the name of the base file or folder from the given path. For example, if the path provided is /var/www/html/project, the output will be project.

For example, use the functions dirname(__FILE__) and getcwd() as the parameters for the basename() function. In this way, we can get the current working directory name in PHP.

echo basename(dirname(__FILE__))."
"; echo basename(getcwd())."\n";

Output:

project
project

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - PHP File

  • Write Into a File in PHP
  • Require_once vs Include in PHP
  • Create and Get the Path of tmpfile in PHP
  • Remove PHP Extension With .Htacess File

    Related Article - PHP Directory

  • Write Into a File in PHP
  • Require_once vs Include in PHP
  • Create and Get the Path of tmpfile in PHP
  • Remove PHP Extension With .Htacess File
  • I have a function that returns the current folder name:

    function the_page_title()
    {
        $page_name = dirname(__FILE__);
        $each_page_name = explode('/', $page_name);
        $len_page_dir = count($each_page_name);
        $c_i_p_n = 0;
        while($len_page_dir != $c_i_p_n)
        {
            $c_i_p_n++;
        }
        echo $each_page_name[$c_i_p_n];
    }
    

    However, this returns what $page_name holds and not the folder I am currently in.

    When I print_r($each_page_name) I get this:

    Array ( [0] => [1] => home [2] => kyleport [3] => public_html [4] => inc ) /home/kyleport/public_html/inc

    Could anyone point me in the right direction because I have no clue where this is going wrong :( Thank-you!

    In this case, I want it to display inc

    Hướng dẫn get current path php

    worldofjr

    3,8098 gold badges34 silver badges48 bronze badges

    asked Feb 18, 2016 at 13:39

    1

    You just want the last element of an array:

    $page_name = dirname(__FILE__);
    $each_page_name = explode('/', $page_name);
    echo end($each_page_name);
    

    answered Feb 18, 2016 at 13:42

    DerVODerVO

    3,6691 gold badge22 silver badges27 bronze badges

    You can use;

    echo getcwd();
    

    This returns the current working directory on success, or FALSE on failure.

    worldofjr

    3,8098 gold badges34 silver badges48 bronze badges

    answered Feb 18, 2016 at 13:48

    safin chackosafin chacko

    1,3521 gold badge10 silver badges18 bronze badges

    As said before, you should use getcwd() or, otherwise, your function will always return the folder where it's located instead of the current script directory.

    Instead of explode, you could use basename;

    function the_page_title()
    {
        $page_name = getcwd(); // current script folder
        return basename($page_name);
    }
    
    <?php echo the_page_title(); ?>
    

    worldofjr

    3,8098 gold badges34 silver badges48 bronze badges

    answered Feb 18, 2016 at 13:57

    CarlosCarucceCarlosCarucce

    3,2581 gold badge28 silver badges48 bronze badges

    You should be able to get the current directory with the predefined constant

    __DIR__
    

    which is the equivalent of

    dirname(__FILE__)
    

    so you should use

    $each_page_name = explode('/', __DIR__);
    $dir = end($each_page_name);
    echo $dir;
    

    answered Feb 18, 2016 at 13:46

    worldofjrworldofjr

    3,8098 gold badges34 silver badges48 bronze badges

    This little line of code did it for me.

    echo basename(getcwd());

    I just got the name of the folder I was looking for.

    answered Mar 24, 2018 at 0:09

    I fixed my code:

    function the_page_title()
    {
        $page_name = getcwd(); // getcwd() gets the directory of the file you call the function from
        $each_page_name = explode('/', $page_name);
        $len_page_dir = count($each_page_name) -1;
        return $each_page_name[$len_page_dir];
    }
    

    use:

    <?php echo the_page_title(); ?>
    

    answered Feb 18, 2016 at 13:44

    JaquarhJaquarh

    6,2595 gold badges28 silver badges73 bronze badges

    2

    Just to add my suggestion to this list. If you simply want to get the current folder name. You could just use __DIR__ in combination with basename()

    
    

    An example of structure and returned content, if the above was added to the file.php:

    ../directory-top/directory-sub/file.php output > directory-sub

    answered Jul 27, 2021 at 11:06

    AaronAaron

    9,9193 gold badges22 silver badges38 bronze badges

    Not the answer you're looking for? Browse other questions tagged php directory or ask your own question.