3

I'm looking for a way to get what the current script running. But the thing is I can't use __FILE__ since it gives me the file i'm currently in and in the case of an included file, I want the initial file.

So if I run index.php and in that file there is an include('test.php') I want to be able in test.php to get 'index.php' as the current file im runing.

I know I could do __FILE__ in index.php and keep the value in an variable. But i'm looking for a way with minimal change to my files.

Also the script is running in CLI on windows. So trying to parse the the path wont work also.

What are my options?

Thanks to everyone taking the time to read this :)

2
  • Everything's inside $_SERVER. Commented Feb 10, 2016 at 16:14
  • 2
    ... or $argv[0] because CLI ... Commented Feb 10, 2016 at 16:17

1 Answer 1

6

You have to use $_SERVER[PHP_SELF].

According to documentation:

The filename of the currently executing script, relative to the document root.

$_SERVER[PHP_SELF]works also on CLI interface. (Edit: ... but I doesn't have tested in Windows)

Sign up to request clarification or add additional context in comments.

2 Comments

Fatal error: Uncaught Error: Undefined constant "PHP_SELF"
PHP_SELF needs to be in quotes - otherwise it will be parsed as a constant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.