I'll set the scene...
- I've got a file example.php
- That particular file is in a folder called test
- The full file path is /root/user/website/htdocs/test/example.php
What I'd like is to echo the directory the file is in ie. test
I've managed to do this by reading the functions and creating the script below, is this a bad way of going about this? is there an easier way?
outputs
test
Thanks in advance.
hek2mgl
145k25 gold badges229 silver badges255 bronze badges
asked Feb 13, 2013 at 17:04
1
PHP has a constant for this:
echo __DIR__;
if you just want to have the last part of the path - test
- in your case, then use basename[]
echo basename[__DIR__];
cryptic ツ
15k9 gold badges53 silver badges80 bronze badges
answered Feb 13, 2013 at 17:08
hek2mglhek2mgl
145k25 gold badges229 silver badges255 bronze badges
1
I'd suggest you the following code:
echo array_pop[explode['/', dirname[__FILE__]]];
If your PHP version is above 5.3.0, you can replace dirname[__FILE__]
with __DIR__
For the problems with getcwd[]
you can read here
answered Feb 13, 2013 at 17:08
RantyRanty
3,2933 gold badges21 silver badges24 bronze badges
Considering this path
store.com/products/electronics/index.php
This also works with include[]
The 3rd combination should work for the OP.
$_SERVER['PHP_SELF'];
/products/electronics/index.php
dirname[$_SERVER['PHP_SELF']];
/products/electronics
basename[dirname[$_SERVER['PHP_SELF']]];
electronics
basename[$_SERVER['PHP_SELF']];
index.php
answered Nov 19, 2013 at 16:12
Vitim.usVitim.us
19k15 gold badges88 silver badges102 bronze badges
Almost:
answered Feb 13, 2013 at 17:08
DanManDanMan
11k4 gold badges39 silver badges58 bronze badges
Hàm currency_format[]
giúp bạn dễ dàng hiện thị dữ liệu có định dạng tiền tệ như VNĐ, USD…
Cú pháp
currency_format[$number[, $suffix = 'đ']]
Input:
$number
: Số cần hiển thị định dạng tiền tệ$suffix
: Đơn vị của tiền tệ đang làm việc như “vnđ”, “USD”…
Output: Dữ liệu hiển thị theo đơn vị tiền tệ được lưa chọn.
Mã code
Kết quả
5.000.000đ
Ví dụ 2: Hiển thị 37
theo định dạng USD
Kết quả
37USD
Tổng kết
Qua bài viết này tôi đã chia sẻ đến bạn mã code của hàm hiển thị dữ liệu theo định dạng tiền tệ. Việc của bạn bây giờ hãy copy nó đưa vào và sử dụng trên dự án của mình.
[PHP 4, PHP 5, PHP 7, PHP 8]
getcwd — Gets the current working directory
Description
getcwd[]: string|false
Parameters
This function has no parameters.
Return Values
Returns the current working directory on success, or false
on failure.
On some Unix variants,
getcwd[] will return false
if any one of the parent directories does not have the readable or search mode set, even if the current directory does. See chmod[] for more information on modes and permissions.
Examples
Example #1 getcwd[] example
The above example will output something similar to:
/home/didou /home/didou/cvs
Caution
If the PHP interpreter has been built with ZTS [Zend Thread Safety] enabled, the current working directory returned by getcwd[] may be different from that returned by operating system interfaces. External libraries [invoked through FFI] which depend on the current working directory will be affected.
dave at corecomm dot us ¶
18 years ago
getcwd[] returns the path of the "main" script referenced in the URL.
dirname[__FILE__] will return the path of the script currently executing.
I had written a script that required several class definition scripts from the same directory. It retrieved them based on filename matches and used getcwd to figure out where they were.
Didn't work so well when I needed to call that first script from a new file in a different directory.
Anonymous ¶
8 months ago
given a link
/some/link->/some/location/path
with linux bash,
if within the linked drawer /some/link
cd .. goes upper link /some/
cd -P .. goes upper destination /some/location/
with php
fopen ["../file"] goes upper destination /some/location/file
some others commented about ways obtaining the path below.
I found some luck with using $_SERVER['DOCUMENT_ROOT'] instead
to recraft an absolute path.
CXJ ¶
5 years ago
getcwd[] appears to call the equivalent of PHP's realpath[] on the path. It never returns symlinks, but always the actual directory names in the path to the current working directory.
znupi69NOSPAMHERE at gmail dot com ¶
14 years ago
When running PHP on the command line, if you want to include another file which is in the same directory as the main script, doing just
might not work, if you run your script like this:
/$ /path/to/script.php
because the current working dir will be set to '/', and the file '/otherfile.php' does not exist, because it is in '/path/to/otherfile.php'.
So, to get the directory in which the script resides, you can use this function:
So you can use it like this:
Spent some time thinking this one out, maybe it helps someone :]
hodgman at ali dot com dot au ¶
15 years ago
I use this code to replicate the pushd and popd DOS commands in PHP:
This allows you to change the current directory with pushd, then use popd to "undo" the directory change when you're done.
memandeemail at gmail dot com ¶
16 years ago
Some server's has security options to block the getcwd[]
Alternate option:
str_replace[$_SERVER['SCRIPT_NAME'],'', $_SERVER['SCRIPT_FILENAME']];
marcus at synchromedia dot co dot uk ¶
17 years ago
"On some Unix variants, getcwd[] will return FALSE if any one of the parent directories does not have the readable or search mode set, even if the current directory does."
Just so you know, MacOS X is one of these variants [at least 10.4 is for me]. You can make it work by applying 'chmod a+rx' to all folders from your site folder upwards.
leonbrussels at gmail dot com ¶
14 years ago
This is a function to convert a path which looks something like this:
/home/www/somefolder/../someotherfolder/../
To a proper directory path:
ab5602 at wayne dot edu ¶
14 years ago
If getcwd[] returns nothing for you under Solaris with an NFS mounted subdirectory, you are running into an OS bug that is supposedly fixed in recent versions of Solaris 10. This same OS bug effects the include[] and require[] functions as well.
emailfire at gmail dot com ¶
16 years ago
To get the username of the account:
If current directory is '/home/mike/public_html/' it would return mike.