How to get the filename of the current file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

How to get the filename of the current file

Post by vigge89 »

How can I get the filename of the current executed script?
IE, i want the script go get it's own filename, formatted "file.ext", no folders or anything else where it's located, just the filename of the file.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[php_man]basename[/php_man]() and $_SERVER['PHP_SELF'].

Mac
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this

Code: Select all

<?

$file = basename ($_SERVER['PHP_SELF']);        

echo "the script is called ".$file;

?>
Mark
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

PHP manual wrote: Table 4.1. A few "magical" PHP constants
Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file.
__FUNCTION__ The function name. (This was added in PHP 4.3.0.)
__CLASS__ The class name. (This was added in PHP 4.3.0.)
__METHOD__ The class method name. (This was added in PHP 5.0.0)
so you can try:

Code: Select all

echo basename(__FILE__);
it will give you true filename even if the file is included from other one. $_SERVER['PHP_SELF'] won't.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

so

Code: Select all

<?php
$file = basename ($_SERVER['PHP_SELF']);
?>
gives me the file running, ie; if i include 1file.php in 2file.php, it will output 2file.php? That's what i want, and I just wanna be sure before i go on with my scipt ;) .
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

test it ;)

Mac
Post Reply