0

url:

localhost/story/  

current file is index.php
Is there a way to get file name (index.php) in php, if it's not part of url.

basename(__FILE__) doesn't work because result is contact.php probably because contact.php is included at the beginning of index.php.

qadenza
  • 9,025
  • 18
  • 73
  • 126

3 Answers3

2

__FILE__ will get current filename

echo basename(__FILE__);

The basename() function returns the filename from a path.

Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
  • @bonaca did you test it? I don't think what you claim is true – eis Oct 31 '16 at 09:41
  • Tested. In this case result is `contact.php` probably because `contact.php` is included at the beginning of `index.php`. – qadenza Oct 31 '16 at 09:46
1
echo basename($_SERVER['PHP_SELF']);
madankundu
  • 154
  • 8
-1

@madankundu answer is ok, but you shouldn't access request variables directly. Better is:

echo basename(filter_input(INPUT_SERVER, 'PHP_SELF'));
webcitron
  • 124
  • 11