vdegenne
vdegenne

Reputation: 13270

How to echo the name of a file from code in an included file

I want to return the name of the first file being processed in the stack.

Let me explain: I've got two files.

1) foo:

include("bar");

2) bar:

echo __ FILE__;

When I request foo it displays "/home/example/bar" while I'd like to see the string "/home/example/foo" happening.

How to do that?

Upvotes: 1

Views: 63

Answers (3)

Daveo
Daveo

Reputation: 1215

This should do the trick:

echo $_SERVER['SCRIPT_FILENAME'];

Upvotes: 0

Marc B
Marc B

Reputation: 360662

The __FILE__ constant by design is ALWAYS the file where the constant appears, regardless of what file(s) have loaded/included this file. Try $_SERVER['SCRIPT_NAME'] instead.

Upvotes: 2

seriousdev
seriousdev

Reputation: 7656

This should do the trick:

$backtrace = debug_backtrace();
echo $backtrace[0]['file'];

http://php.net/manual/en/function.debug-backtrace.php

Upvotes: 1

Related Questions