If I have PHP script, how can I get the filename from inside that script?
Also, given the name of a script of the form jquery.js.php
, how can I extract just the "jquery.js" part?
If I have PHP script, how can I get the filename from inside that script?
Also, given the name of a script of the form jquery.js.php
, how can I extract just the "jquery.js" part?
__FILE__
use examples based on localhost server results:Here is the difference between
basename(__FILE__, ".php")
andbasename($_SERVER['REQUEST_URI'], ".php")
.basename(__FILE__, ".php")
shows the name of the file where this code is included - It means that if you include this code in header.php and current page is index.php, it will return header not index.basename($_SERVER["REQUEST_URI"], ".php")
- If you use include this code in header.php and current page is index.php, it will return index not header.you can also use this:
As some said
basename($_SERVER["SCRIPT_FILENAME"], '.php')
andbasename( __FILE__, '.php')
are good ways to test this.To me using the second was the solution for some validation instructions I was making
This might help:
it will work even if you are using include.
See http://php.net/manual/en/function.pathinfo.php