PHP getFile() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getFile() returns the path of the PHP file where the exception object was created. That is usually the file that ran throw new Exception(...).

Use getFile() with getLine() when you write debug logs. The method is final. The path is set automatically when the exception is constructed, so you do not pass the file name yourself.

Syntax

Call getFile() with the values you need. Start with this line.

PHP
$file = $e->getFile();
CallWhat it does
$e->getFile()Returns the file path where the exception was created.
basename($e->getFile())Shows only the file name without the folder path.
$e->getFile() . ":" . $e->getLine()Builds a common file:line location string.

getFile() is final and filled in by PHP when the Exception is constructed. It reports creation location, not necessarily where the exception was caught.

Library declarations
PHP
final public Exception::getFile(): string

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
stringAbsolute or project path of the file that created the exception.

PHP Examples

Example 1 — Print File and Line

Catch an Exception and show where it was thrown using getFile() and getLine().

PHP
<?php
try {
    throw new Exception("Demo failure");
} catch (Exception $e) {
    echo "Has .php: ", str_ends_with($e->getFile(), ".php") ? "yes" : "no", PHP_EOL;
    echo "Line: ", $e->getLine(), PHP_EOL;
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. throw creates the Exception on a specific line of this script.
  2. getFile() returns the full path of that script.
  3. str_ends_with() confirms the path points at a . php file.
  4. getLine() returns the throw line number.

Example 2 — Created in a Function

Show that getFile() points at the throw site inside a helper function.

PHP
<?php
function fail(): never
{
    throw new Exception("Helper failed");
}

try {
    fail();
} catch (Exception $e) {
    echo "Line inside fail(): ", $e->getLine(), PHP_EOL;
    echo "Path is string: ", is_string($e->getFile()) ? "yes" : "no", PHP_EOL;
}

How It Works

  1. fail() throws the Exception.
  2. getLine() refers to the throw line inside fail(), not the call site.
  3. getFile() still returns this script’s path as a string.
  4. Use getTrace() if you also need the caller’s location.

Example 3 — Build a Short Log Prefix

Combine getFile(), getLine(), and getMessage() into one log-style line.

PHP
<?php
try {
    throw new Exception("Cannot write cache");
} catch (Exception $e) {
    $where = basename($e->getFile()) . ":" . $e->getLine();
    echo str_contains($where, ":") ? "ok" : "bad", PHP_EOL;
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. basename(getFile()) shortens the path.
  2. getLine() supplies the line number.
  3. The combined location uses a colon between file and line.
  4. getMessage() prints the reason on the next line.

Frequently Asked Questions

The path of the PHP file where the exception object was created, usually the file that executed throw.
No. It shows where the Exception was constructed. The catch location is not stored on the object.
No. The method is final.
Full paths can be long and may expose server folders. basename() keeps only the file name for safer or shorter output.
getFile() is a single creation path. getTrace() is an array of call frames that led to the throw.

Next: getLine()

Exception::getLine() returns the line number where the exception object was created. Toget.

getLine() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful