Get absolute path of initially run script

Get absolute path of initially run script

  1. Blog
  2. 15 November 2026
  3. 1 min read

Stefan Bogdanescu

Founder & Senior Architect

The Silver Bullet for PHP Script Paths Does Not Exist

Developers frequently encounter a specific requirement where they need to identify the absolute filesystem path of the script that initiated execution. Whether you are deploying legacy scripts via CLI or integrating logic into an Apache environment, the need persists: how do you reliably determine file_get_contents('/path/to/your/script') without relying on environment variables that might be spoofed or incomplete?

The short answer is that there is no universal "silver bullet" function in PHP that guarantees this across every deployment scenario. The solution depends entirely on distinguishing between the entry point of the application and the current file stack.

Understanding __FILE__ vs. $_SERVER['SCRIPT_FILENAME']

When code runs inside a file, the superglobal $_SERVER['SCRIPT_FILENAME'] typically contains the absolute path to that specific file. However, this variable only points to the entry point if invoked from the command line or when Apache serves the file directly as the request target. If you include another file using require, include, or even a simple <script src="..."> in HTML, $_SERVER['SCRIPT_FILENAME'] will still reference the original entry script, not the included one.

For example:

<?php
// file.php (Entry Point)
echo realpath($_SERVER['SCRIPT_FILENAME']); 
// Outputs: /var/www/html/app/file.php

require 'helper.php';

// helper.php (Included File)
echo realpath($_SERVER['SCRIPT_FILENAME']); 
// Still outputs: /var/www/html/app/file.php

This behavior is consistent and predictable, but it fails the specific constraint of identifying the "initially run script" when that script is distinct from the file where your logic resides. In complex architectures involving multiple entry points or dynamic loading, $_SERVER['SCRIPT_FILENAME'] might not align with your logical application root if you are relying on it to represent the current context rather than the origin.

The get_included_files() Nuance

To isolate the initially executed script regardless of where your code currently sits in the stack, you must inspect the file inclusion history. PHP maintains an internal array of all files included or required during execution.

You can retrieve this list using get_included_files(). This function returns an array of absolute paths to every file involved in the current request. To find the entry point, you simply look for the first element in this array.

<?php
$files = get_included_files();

// The first file listed is almost always the initial script executed
$initialPath = $files[0];

echo realpath($initialPath); 

This approach works reliably for CLI scripts because the entry point is explicitly loaded first. It also functions correctly within Apache environments, provided the application does not utilize a framework bootstrap mechanism that loads a configuration file before the main controller logic. If you are using a standard Laravel installation, for instance, bootstrap/app.php will be the first file in this list, and your actual entry script (e.g., public/index.php) might be second. In such cases, the "initially run script" is technically the bootstrap loader, not your specific feature file.

Trade-offs and Framework Considerations

The primary trade-off with relying on get_included_files()[0] is that it identifies the physical entry point of the PHP process, which may be a generic router or bootstrapper rather than your specific business logic script. If your architecture wraps every request in a common bootstrap file, this method returns the path to that wrapper.

If you need the absolute path of a specific controller or module within a framework like Laravel, relying on $_SERVER['SCRIPT_FILENAME'] is often insufficient because it refers to the public entry point (public/index.php). Instead, frameworks usually provide context-aware methods. In Laravel, for example, you might use app()->runningInConsole() to determine if you are in CLI mode and access the kernel or application instance to resolve paths dynamically based on the route or command being executed.

For raw PHP scripts without a framework, get_included_files()[0] remains the most robust method to identify the very first file parsed by the interpreter. However, developers must be vigilant about auto-loading configurations that might prepend files before their intended entry logic runs. Always verify that your bootstrap sequence matches your definition of "initially run" before hard-coding assumptions into your path resolution logic.

Our network