- Php php get the name of the script
- Get the name of the including script [duplicate]
- How can I get the name of the script that called a function?
- Php — get parent-script name
- PHP Exercises : Get the name of the owner of the current PHP script
- PHP : Exercise-24 with Solution
- Finding out the filename that called my function in PHP
- 6 Answers 6
- How to get current server path, url domain, filename or script name in PHP
- 2 Answers 2
- How can I get the current script file name, but without the extension «.php»?
- 18 Answers 18
Php php get the name of the script
If you want to know which PHP script was initially called (which URL was requested, for instance), you might have more luck looking at the superglobal : it contains many informations, including some that will help you (like or , for instance) 😉 PHP : Exercise-24 with Solution Write a PHP script to get the name of the owner of the current PHP script. Solution: From an answer to Stack Overflow question Get name of file that is including a PHP script : Solution 1: Depending on the calling environment, you should have a look at debug_backtrace, and $_SERVER[‘PHP_SELF’] will give you a stack trace of the includes and function calls so far, and will tell you the currently executing script, which is easier and might work just as well for what you want. will pretty much always be the script that was called from the browser, for example, if you had and that both called to get a list of the pages saved in a blog or something, and something went wrong — the log would tell you whether or was calling the script.
Get the name of the including script [duplicate]
From an answer to stack overflow question Get name of file that is including a PHP script :
How to access the form’s ‘name’ variable from PHP, I’m trying to create a BMI calculator. This should allow people to use either metric or imperial measurements. I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I’d ask: I can use $_POST[‘variableName’] to find the submitted variableName field-value; butI …
How can I get the name of the script that called a function?
Depending on the calling environment, you should have a look at debug_backtrace, and $_SERVER[‘PHP_SELF’]
debug_backtrace() will give you a stack trace of the includes and function calls so far, and $_SERVER[‘PHP_SELF’] will tell you the currently executing script, which is easier and might work just as well for what you want. $_SERVER[‘PHP_SELF’] will pretty much always be the script that was called from the browser, for example, if you had blah.com/admin.php and blah.com/articles.php that both called /pages.php to get a list of the pages saved in a blog or something, and something went wrong — the log would tell you whether admin.php or articles.php was calling the script. But if pages.php included functions.php which included libs.php and libs.php failed, and you wanted to know that functions.php included it, this wouldn’t work — the log would still show the script that started the including ( admin.php or articles.php ). In this case you would use debug_backtrace() .
You can take a look at the debug_backtrace function : in it’s output, you should find what you are looking for 😉
You can take a look to this answer I posted a couple of days ago, for more informations, and an example.
Take a look at debug_baktrace.
$backtrace = debug_backtrace(); print_r($backtrace[1]);
How can I get the current PHP executable from within a, php5 outer.php I want the inner script to be run with the same PHP version. In Perl, I would use $^X to get the Perl executable. It appears there isn’t any such variable in PHP. Right now, I’m using $_SERVER[‘_’], because Bash (and zsh) sets the environment variable $_ to the last-run program. But, I’d rather not rely on a …
Php — get parent-script name
The chosen answer only works in environments that set server variables and specifically won’t work from a CLI script. Furthermore, it doesn’t determine the parent, but only the topmost script file.
You can do almost the same thing from a CLI script by looking at $argv[0], but that doesn’t provide the full path.
The environment-independent solution uses debug_backtrace :
function get_topmost_script()
print $_SERVER["SCRIPT_FILENAME"];
I don’t think you can do that : the __FILE__ magic constant indicates in which file it is written ; and that is all.
If you want to know which PHP script was initially called (which URL was requested, for instance), you might have more luck looking at the $_SERVER superglobal : it contains many informations, including some that will help you (like SCRIPT_FILENAME or SCRIPT_NAME , for instance) 😉
Php — get parent-script name, The other way to do this (works under both Apache and CLI) is by using the get_included_files () function. It returns an array with all of the included files (including the initial calling script) in the order they were run. get_included_files () [0] = full path of initial script. E.g. /myscripts/Parent.php.
PHP Exercises : Get the name of the owner of the current PHP script
PHP : Exercise-24 with Solution
Write a PHP script to get the name of the owner of the current PHP script.
Sample Solution: —
Current script owner: students
Note: get_current_user — Gets the name of the owner of the current PHP script.
PHP Code Editor:
PHP Exercise: Get the current file name, PHP : Exercise-7 with Solution. Write a PHP script to get the current file name. Computer file: A computer file is a computer resource on a computer that stores data, information, picture, video, settings, or commands used with a computer program. In a graphical user interface an operating system displays a …
Finding out the filename that called my function in PHP
Could you please give more info about why and to what purpose you would need such functionality? Might be that you are approaching a problem from the wrong end.
To give an example: require with relative paths loads files from the root dir, even if you invoke require in a subdirectory. Having to prepend DIR to every require call could be hidden away with a wrapper function, but then DIR would point to the wrapper function’s directory, not the callee’s.
6 Answers 6
A solution might be to use the debug_backtrace function : in the backtrace, that kind of information should be present.
Or, as Gordon pointed out in a comment, you can also use debug_print_backtrace if you just want to output that information and not work with it.
For instance, with temp.php containing this :
and with temp-2.php containing this :
Calling temp.php (i.e. the first script) from my browser gets me this output :
array 0 => array 'file' => string '/. /temp/temp.php' (length=46) 'line' => int 5 'function' => string 'my_function' (length=11) 'args' => array empty
In there, I have the » temp.php » filename — which is the one in which the function has been called.
Of course, you’ll have to test a bit more (especially in situations where the function is not in the «first level» included file, but in a file included by another one — not sure debug_backtrace will help much, there. ) ; but this might help you get a first idea.
How to get current server path, url domain, filename or script name in PHP
When working with a script in PHP, I need to know what its current folder and path is, both from the server point of view and the domain url point of view. I also need the filename or script name with and without the extension, and also with and without any arguments passed in. For example, if I call the following: https://example.com/somefolder/anotherfolder/test.php?hey=there I want to get back all these individual parts:
test test.php test.php?hey=there hey=there https://example.com /home/myserver/public_html /somefolder/anotherfolder
2 Answers 2
So this is my generic solution that will break it all down and then allow you to piece everything back together as needed.
See output below for putting script at this following location (and then calling it): https://example.com/somefolder/anotherfolder/test.php?hey=there
"; // domain url including path $domain_with_path = $domain_no_path.$path_only; // domain url and path and filename with extension $full_domain = $domain_with_path."/".$script_name_with_ext; // server including path $server_with_path = str_replace ("/".$script_name_with_ext , "", __FILE__); // server only with no path $server_no_path = str_replace($path_only, "", $server_with_path); // server and path and filename with extension $full_server = $server_with_path."/".$script_name_with_ext; // output everything here echo $script_name_no_ext."
"; echo $script_name_with_ext."
"; echo $script_name_with_args."
"; echo $args_no_script_name."
"; echo $path_only."
"; echo $server_no_path."
"; echo $server_with_path."
"; echo $full_server."
"; echo $full_server."?".$args_no_script_name."
"; echo $domain_no_path."
"; echo $domain_with_path."
"; echo $full_domain."
"; echo $full_domain."?".$args_no_script_name."
"; ?>
The output will look like this:
test test.php test.php?hey=there hey=there /somefolder/anotherfolder /home/myserver/public_html /home/myserver/public_html/somefolder/anotherfolder /home/myserver/public_html/somefolder/anotherfolder/test.php /home/myserver/public_html/somefolder/anotherfolder/test.php?hey=there https://example.com https://example.com/somefolder/anotherfolder https://example.com/somefolder/anotherfolder/test.php https://example.com/somefolder/anotherfolder/test.php?hey=there
How can I get the current script file name, but without the extension «.php»?
If I have PHP script, how can I get the filename of the currently executed file without its extension? Given the name of a script of the form «jquery.js.php», how can I extract just the «jquery.js» part?
18 Answers 18
Just use the PHP magic constant __FILE__ to get the current filename.
But it seems you want the part without .php . So.
A more generic file extension remover would look like this.
function chopExtension($filename) < return pathinfo($filename, PATHINFO_FILENAME); >var_dump(chopExtension('bob.php')); // string(3) "bob" var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"
Using standard string library functions is much quicker, as you’d expect.
function chopExtension($filename)
@ThiefMaster Because there is something built into PHP to handle file extensions. The wheel exists, and rolls well.
While __FILE__ gives you the .php file that line is in, you actually want $_SERVER[‘SCRIPT_NAME’] for the currently running top-level script (that which was invoked by the web server or directly on the command line)
When you want your include to know what file it is in (ie. what script name was actually requested), use:
basename($_SERVER["SCRIPT_FILENAME"], '.php')
Because when you are writing to a file you usually know its name.
Edit: As noted by Alec Teal, if you use symlinks it will show the symlink name instead.
This is wrong, it wont get the actual PHP file, but the file the webserver resolved to the request. Different if you use simlinks.
pathinfo(__FILE__, PATHINFO_FILENAME);
Here is the difference between basename(__FILE__, «.php») and basename($_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.
which is safer SCRIPT_FILENAME or REQUEST_URI ? I know they both are server vars but isn’t REQUEST_URI a user tampered value? it enables a «URI injection» threat
both have their own impotence, But you can safe your url using different filers, like mysql_real_escape_string, stripslashes etc..
@KhandadNiazi basename($_SERVER[«REQUEST_URI»], «.php»); will return the folder’s name if the link is of the form http://example.com/somefolder . While basename($_SERVER[‘PHP_SELF’], «.php»); will always return the script’s name, in this case index .
it will work even if you are using include.
I have 2 files, header.php and home.php, the header.php is called in home.php how can i detect in header that the current page is home.php or contact.php so i could change some banner etc.
Here is a list what I’ve found recently searching an answer:
//self name with file extension echo basename(__FILE__) . '
'; //self name without file extension echo basename(__FILE__, '.php') . '
'; //self full url with file extension echo __FILE__ . '
'; //parent file parent folder name echo basename($_SERVER["REQUEST_URI"]) . '
'; //parent file parent folder name with //s echo $_SERVER["REQUEST_URI"] . '
'; // parent file name without file extension echo basename($_SERVER['PHP_SELF'], ".php") . '
'; // parent file name with file extension echo basename($_SERVER['PHP_SELF']) . '
'; // parent file relative url with file etension echo $_SERVER['PHP_SELF'] . '
'; // parent file name without file extension echo basename($_SERVER["SCRIPT_FILENAME"], '.php') . '
'; // parent file name with file extension echo basename($_SERVER["SCRIPT_FILENAME"]) . '
'; // parent file full url with file extension echo $_SERVER["SCRIPT_FILENAME"] . '
'; //self name without file extension echo pathinfo(__FILE__, PATHINFO_FILENAME) . '
'; //self file extension echo pathinfo(__FILE__, PATHINFO_EXTENSION) . '
'; // parent file name with file extension echo basename($_SERVER['SCRIPT_NAME']);