Hướng dẫn get current path php
Created: December-04, 2021 Nội dung chính
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 PHPThe The function does not take any parameters. The function returns false in case of failure. Let’s consider the following directory structure.
The PHP file lies inside the We can use the
Output:
Use the dirname() Function to Get the Current Directory Name in PHPWe can also use the 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 We can
demonstrate these constants and the function in the above directory structure. For example, we get the following result when we echo the
Output:
For example, write the
Output:
In this way, we can get the current working directory name in PHP. Use the basename() Function to Get the Current Directory Name in PHPWe can use the The For example, use the functions
Output:
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 FileRelated Article - PHP DirectoryI have a function that returns the current folder name:
However, this returns what When I
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 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:
answered Feb 18, 2016 at 13:42 DerVODerVO 3,6691 gold badge22 silver badges27 bronze badges You can use;
This returns the current working directory on success, or 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 Instead of explode, you could use
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
which is the equivalent of
so you should use
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.
I just got the name of the folder I was looking for. answered Mar 24, 2018 at 0:09 I fixed my code:
use:
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
An example of structure and returned content, if the above was added to the file.php:
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. |