Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the name of file in PHP

Tags:

php

How can I get the name of a file in in PHP? What I want to do is have a function take a string of a filename and then do some stuff if it's actually on the page.

function onThisPageFunction(fileNameFromFunction)  
{  
  if(onThisPage == fileNameFromFunction)  
  {  
    do stuff  
  }  
}
like image 739
Paul Sheldrake Avatar asked Nov 19 '25 22:11

Paul Sheldrake


1 Answers

There are two variables that will give you file names.

eg: you call /test/test1.php as the url and test1.php includes test2.php.

__FILE__ will give you the current file of the executing code, in this case it will return "test2.php"

$_SERVER['PHP_SELF'] will give you the full url of the original call, in this case "/test/test1.php"

Note that you can use the basename() function to get just the filename from a path.

like image 184
Eric Goodwin Avatar answered Nov 21 '25 14:11

Eric Goodwin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!